#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a[100005],kq[100005],d[100005],t = 0,x = 0,kt;
vector <int> ve;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("TREAT.inp", "r", stdin);
//freopen("TREAT.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
kq[i] = 0;
if(a[i] == i)
kq[i] = 1;
d[i] = 0;
}
for(int i = 1; i <= n; i++)
{
if(!kq[i])
{
ll j = i;
ve.push_back(i);
d[i]++;
t = 1;
x = 0;
kt = 0;
while(d[j] <= 1)
{
j = a[j];
ve.push_back(j);
d[j]++;
if(kq[j] != 0)
{
x = 1;
t += kq[j];
break;
}
if(d[j] <= 1)
t++;
else kt = j;
}
ll y = 0,tam = 0;
for(auto k : ve)
{
if(k == kt && y == 0)
{
y = 1;
kq[k]= t - tam;
}
else if(y == 1)
{
kq[k]= t - tam;
}
else if(k == kt && y == 1)
{
y = 0;
kq[k]= t - tam;
}
else
{
kq[k] = t - tam;
tam++;
}
d[k] = 0;
}
kq[i] = t;
ve.clear();
}
}
//cout << '\n';
for(int i = 1; i <= n; i++)
cout << kq[i] << '\n';
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKbGwgbixhWzEwMDAwNV0sa3FbMTAwMDA1XSxkWzEwMDAwNV0sdCA9IDAseCA9IDAsa3Q7CnZlY3RvciA8aW50PiB2ZTsKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIC8vZnJlb3BlbigiVFJFQVQuaW5wIiwgInIiLCBzdGRpbik7CiAgICAvL2ZyZW9wZW4oIlRSRUFULm91dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIGNpbiA+PiBuOwogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICAgICAga3FbaV0gPSAwOwogICAgICAgIGlmKGFbaV0gPT0gaSkKICAgICAgICAgICAga3FbaV0gPSAxOwogICAgICAgIGRbaV0gPSAwOwogICAgfQogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICB7CiAgICAgICAgaWYoIWtxW2ldKQogICAgICAgIHsKICAgICAgICAgICAgbGwgaiA9IGk7CiAgICAgICAgICAgIHZlLnB1c2hfYmFjayhpKTsKICAgICAgICAgICAgZFtpXSsrOwogICAgICAgICAgICB0ID0gMTsKICAgICAgICAgICAgeCA9IDA7CiAgICAgICAgICAgIGt0ID0gMDsKICAgICAgICAgICAgd2hpbGUoZFtqXSA8PSAxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBqID0gYVtqXTsKICAgICAgICAgICAgICAgIHZlLnB1c2hfYmFjayhqKTsKICAgICAgICAgICAgICAgIGRbal0rKzsKICAgICAgICAgICAgICAgIGlmKGtxW2pdICE9IDApCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgeCA9IDE7CiAgICAgICAgICAgICAgICAgICAgdCArPSBrcVtqXTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKGRbal0gPD0gMSkKICAgICAgICAgICAgICAgICAgICB0Kys7CiAgICAgICAgICAgICAgICBlbHNlIGt0ID0gajsKICAgICAgICAgICAgfQogICAgICAgICAgICBsbCB5ID0gMCx0YW0gPSAwOwogICAgICAgICAgICBmb3IoYXV0byBrIDogdmUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZihrID09IGt0ICYmIHkgPT0gMCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHkgPSAxOwogICAgICAgICAgICAgICAgICAgICAgICBrcVtrXT0gdCAtIHRhbTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZWxzZSBpZih5ID09IDEpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBrcVtrXT0gdCAtIHRhbTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZWxzZSBpZihrID09IGt0ICYmIHkgPT0gMSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHkgPSAwOwogICAgICAgICAgICAgICAgICAgICAgICBrcVtrXT0gdCAtIHRhbTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAga3Fba10gPSB0IC0gdGFtOwogICAgICAgICAgICAgICAgICAgICAgICB0YW0rKzsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBkW2tdID0gMDsKICAgICAgICAgICAgfQogICAgICAgICAgICBrcVtpXSA9IHQ7CiAgICAgICAgICAgIHZlLmNsZWFyKCk7CiAgICAgICAgfQogICAgfQogICAgLy9jb3V0IDw8ICdcbic7CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgICAgICBjb3V0IDw8IGtxW2ldIDw8ICdcbic7Cn0K