#include #include using namespace std; int main(int argc, char** argv) { ios::sync_with_stdio(false); int N, x; while(cin >> N) { vector a(N); for(int i = 0; i < N; ++i) { cin >> x; a[i] = x-1; } int res = 0; vector visited(N, false); for(int i = 0; i < N; ++i) { if (!visited[i]) { visited[i] = true; int y = a[i], l =0; while(y != i) { l+=1; visited[y] = true; y = a[y]; } // cout << l << endl; res += l; } } cout << res << endl; // int n = 0, t = 0; // for(int i = 0; i < N; ++i) { //// cout << n << " " << t << '\t' << i << " " << a[i] << endl; // if (a[i] == i) continue; // if (i == a[a[i]]) { // if (i < a[i]) {n += 1;continue;} // else continue; // } // t++; //// cout << i << " " << a[i] << " " << n << " " << t << endl; // // // } //// cout << endl << endl; //// cout << n << " "<< t << endl; // if (t == 1) cout << n+1 << endl; // else cout << (n + max(0, t-1)) << endl; } }