#include using namespace std; #define FOR(i,a,b) for(auto i=a;i; using vvi=vector; using pii=pair; int main(){ ios::sync_with_stdio(false);cout.tie(0);cin.tie(0); while (1) { int n; cin >> n; if (!cin) break; vector a(n + 1); for (int i = 1; i <= n; ++ i) cin >> a[i]; ll r = 0; vector v(n + 1, false); for (int i = 1; i <= n; ++ i) { if (v[i]) continue; int c = i; int cc = 1; if (c == a[i]) { v[i] = true; continue; } while (!v[c]) { cc ++; v[c] = true; c = a[c]; } r += cc - 2; v[c] = true; } cout << r << endl; } return 0; }