#include #include #include int a[30000]; std::unordered_set ok; int main() { int n, foo, moves, act; while(scanf(" %d", &n) == 1) { for (int i = 1; i <= n; i++) { scanf(" %d", &(a[i])); if (a[i] != i) ok.insert(i); } moves = 0; // while there are elements to visit: while (!ok.empty()){ act = ok.extract(ok.begin()).value(); foo = act; while (a[foo] != act) { foo = a[foo]; ok.extract(a[foo]); moves++; } } printf("%d\n", moves); } return 0; }