#include <cstdint>
#include <iostream>
#include <vector>
using i64 = int64_t;
void solve() {
int N;
std::cin >> N;
std::vector<int> next(N + 1), vis(N + 1);
std::vector<std::vector<int>> rnext(N * 2 + 1);
for (int u = 1; u <= N; u++) {
int v;
std::cin >> v;
next[u] = v;
rnext[v].push_back(u);
}
auto f = [&] (auto &self, int u) -> std::pair<int, int> {
if (u <= N) vis[u] = true;
int size = 0, maxh = 0;
for (auto v : rnext[u]) {
auto [sz, mh] = self(self, v);
size += sz;
maxh = std::max(maxh, mh);
}
return std::pair{size + 1, maxh + 1};
};
int lucky = 0;
for (int p = N + 1; p <= N * 2; p++) {
auto [size, maxh] = f(f, p);
lucky += maxh - 1;
}
auto g = [&] (int start) -> int {
if (vis[start]) return 0;
std::vector<int> cur_vis(N + 1);
int p = start;
while (true) {
if (vis[p]) {
int q = start;
while (q != p) { vis[q] = true; q = next[q]; }
return 0;
}
if (cur_vis[p]) {
int ring_len = 1; vis[p] = true; int q = next[p];
while (q != p) { ring_len += 1; vis[q] = true; q = next[q]; }
q = start;
while (q != p) { vis[q] = true; q = next[q]; }
return ring_len;
}
cur_vis[p] = true;
p = next[p];
}
return 0;
};
for (int p = 1; p <= N; p++) {
if (vis[p]) continue;
lucky += g(p);
}
std::cout << lucky << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// std::cin >> T;
while (T--) {
solve();
}
}