#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=1000010;
int n,m,ans,now,cnt;
int to[maxn],next[maxn],head[maxn],f[maxn],g[maxn],fa[maxn],ra[maxn],rb[maxn];
int find(int x)
{
return (fa[x]==x)?x:(fa[x]=find(fa[x]));
}
void dfs(int x)
{
int i,t=1<<30;
g[x]=0;
for(i=head[x];i!=-1;i=next[i])
{
if(to[i]!=now) dfs(to[i]);
g[x]+=max(f[to[i]],g[to[i]]);
t=min(t,max(f[to[i]],g[to[i]])-g[to[i]]);
}
f[x]=g[x]+1-t;
}
void add(int a,int b)
{
to[cnt]=b;
next[cnt]=head[a];
head[a]=cnt++;
}
int main()
{
scanf("%d",&n);
int i,a;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++) fa[i]=i;
for(i=1;i<=n;i++)
{
scanf("%d",&a);
if(find(a)!=find(i))
{
add(a,i);
fa[fa[a]]=fa[i];
}
else ra[++m]=a,rb[m]=i;
}
for(i=1;i<=m;i++)
{
dfs(ra[i]),now=ra[i];
dfs(rb[i]),a=f[rb[i]];
f[ra[i]]=g[ra[i]]+1;
dfs(rb[i]),ans+=max(a,g[rb[i]]);
}
printf("%d",ans);
return 0;
}