#include #include int a[30000]; int ok[30000]; int main() { int n, foo, moves; while(scanf(" %d", &n) == 1) { for (int i = 1; i <= n; i++) { scanf(" %d", &(a[i])); if (a[i] == i) ok[i] = 1; else ok[i] = 0; } moves = 0; // go through every element: for (int i = 1; i <= n; i++) { // if item is already a part of a cycle / solved if (ok[i] == 1) continue; foo = i; ok[i] = 1; while (a[foo] != i) { foo = a[foo]; ok[foo] = 1; moves++; } } printf("%d\n", moves); } return 0; }