#include #include using namespace std; using namespace atcoder; using ll = long long; using mint=modint998244353; using ld = long double; const ll infl = 1LL << 60; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const vector dx = {1, 0, -1, 0}; const vector dy = {0, 1, 0, -1}; template using vc = vector; template using vvc = vc>; template using vvvc = vc>; using vi = vc; using vvi = vvc; using vl = vc; using vvl = vvc; using vvvl = vvc; using vvvvl = vvc; using vs = vc; using vvs = vvc; using P = pair; #define nrep(i,n) for (ll i = 0; i < (n); ++i) #define nfor(i,s,n) for(ll i=s;i using pq = priority_queue>;//★大きい順に取り出す コスト,頂点 bfs系で使う 小さい順じゃないですABC305E template using pq_g = priority_queue, greater>;//小さい順に取り出す ダイクストラ法で使う #define cout(n) cout<> N; dist.resize(N+1, infl); g.resize(N+1); set s; nrep(i, N){ ll x; cin >> x; if(x == 0) continue; g[i+1].push_back(x); g[x].push_back(i+1); s.insert(x); } vl roots; for (ll i = 1; i <= N; ++i) { if (s.find(i) == s.end()) { roots.push_back(i); dist[i] = 0; dfs(i, -1, 0); } } ll ans = 0; nfor(i, 1, N+1) { ans += (dist[i]==2); } cout(ans); // vc_cout(dist); return 0; }