#include <bits/stdc++.h>
#define ll int
#define FOR(index, lower, upper) for (ll index = lower; index < upper; index++)
using namespace std;
void solve(ll);
void io() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
io();
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
ll T = 1ll;
// cin >> T;
FOR(i, 1, T + 1) {
solve(i);
}
}
ll n;
vector<ll> vc;
ll size_of_cycle = 0;
vector<ll> ans;
void dfs(ll node, vector<ll> &vc, vector<bool> &part_of_cycle, vector<ll> &ans) {
if (part_of_cycle[node]) {
ans[node] = size_of_cycle;
return;
}
dfs(vc[node], vc, part_of_cycle, ans);
ans[node] = ans[vc[node]] + 1;
}
void helper(ll src) {
ll hare = src, tortoise = src;
vector<bool> visited(n + 1, false);
visited[hare]=true;
do{
hare = vc[vc[hare]];
tortoise = vc[tortoise];
visited[hare] = true;
visited[tortoise] = true;
}while (hare != tortoise);
tortoise = src;
while (hare != tortoise) {
hare = vc[hare];
tortoise = vc[tortoise];
visited[hare] = true;
visited[tortoise] = true;
}
ll t = hare;
vector<bool> part_of_cycle(n + 1, false);
size_of_cycle=0;
do {
part_of_cycle[t] = true;
t = vc[t];
size_of_cycle++;
} while (t != hare);
FOR(i, 1, n + 1) {
if (ans[i] == -1 && visited[i]) {
dfs(i, vc, part_of_cycle, ans);
}
}
}
void solve(ll TC) {
cin >> n;
vc.resize(n+1);
FOR(i,1,n+1){
cin>>vc[i];
}
ans.resize(n + 1, -1);
FOR(i, 1, n + 1) {
if (ans[i] == -1)
helper(i);
}
FOR(i,1,n+1)cout<<ans[i]<<" ";
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgaW50CiNkZWZpbmUgRk9SKGluZGV4LCBsb3dlciwgdXBwZXIpIGZvciAobGwgaW5kZXggPSBsb3dlcjsgaW5kZXggPCB1cHBlcjsgaW5kZXgrKykKdXNpbmcgbmFtZXNwYWNlIHN0Z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