#include #include #define MAX 300000 #define LL long long using namespace std; int main() { std::iostream::sync_with_stdio(false); LL N; vector perm(MAX); vector vis(MAX, 0); LL f = 1; while (cin >> N) { for (int i = 1; i <= N; ++i) { cin >> perm[i]; } LL swaps = 0; for (int i = 1; i <= N; ++i) { if (vis[i] == f) { continue; } vis[i] = f; LL next = perm[i]; while (next != i) { ++swaps; vis[next] = f; next = perm[next]; } } cout << swaps << endl; ++f; } return 0; }