#include<bits/stdc++.h>
using namespace std;
#define err(x) cout<<#x<<"= "<<x<<endl;
#define FOR(i,a,b) for(int i =a; i< b; ++i)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define INF 1000000000
#define TRVI(it,it1,it2) for(vi::iterator it = it1; it!= it2; it++)
#define ff first
#define ss second
#define mp make_pair
#define pq priority_queue<int, vector<int>, greater<int> >
#define ll long long
const ll PR = 1000000009;
#define SIZE 1000005
#define vi vector<int>
#define pii pair<int,int>
#define endl '\n'
#define vii vector<ii >
#define ii pair<int,int>

vi visited(SIZE,0),llist[SIZE];
int done[SIZE] = {0},size[SIZE] = {0},clevel[SIZE] = {0};

int findsizes(int v){
	visited[v]++;
	size[v]  =1;
	for(int i= 0; i< llist[v].size(); i++){
		int nxt = llist[v][i];
		if(!done[nxt] &&visited[nxt]<visited[v])
			size[v]+= findsizes(nxt);
	}
	return size[v];
}
int findcentroid(int v,int root){
	int tot = size[root];
	for(int i =0; i< llist[v].size(); i++){
		int nxt = llist[v][i];
		if(!done[nxt]){
			int nows = size[nxt];
			if(size[nxt] > size[v])
				nows = tot-size[v];
			if(nows>tot/2.0){
				return findcentroid(nxt,root);
			}
		}
	}
	return v;
}

//vi clist[SIZE];
int croot,cpar[SIZE];
int idsc[SIZE][21];
int part;
vector<int> Dists[SIZE];
int construct_ctree(int rootnow,int fl=0){             // returns the root
	findsizes(rootnow);
//	findsizes(rootnow);
	int c = findcentroid(rootnow,rootnow);
	if(fl==1)clevel[c ] = 0;
	else
	clevel[c] = clevel[part]+1;
	done[c] =1;

	for(int i =0; i< llist[c].size(); i++){
		int nxt = llist[c][i];
		if(!done[nxt]){
			part = c;
			nxt = construct_ctree(nxt);
//			clist[c].pb(nxt);
//			clist[nxt].pb(c);
			cpar[nxt ] =c;
		}
	}

	queue<ii > Q;int nowl = clevel[c];
	Q.push(mp(c,0) );
//	idsc[c][nowl] = 0;
//	Dists[c].pb(0);
//	cout<<"for c = "<<c<<endl;
	while(!Q.empty()){
		ii now = Q.front();Q.pop();
		int nxt = now.ff,distf = now.ss;
//		if(idsc[nxt][nowl]!=-1)cout<<"Hey error buddy"<<endl;
		idsc[nxt][nowl] = Dists[c].size()+1;
		Dists[c].pb(distf);
//		cout<<nxt<<' '<<distf<<endl;
		for(int i =0; i< llist[nxt].size(); i++){
			int nxn = llist[nxt][i];
			if(idsc[nxn][nowl]==0 && clevel[nxn]>nowl){
				Q.push(mp(nxn,distf+1));
			}
		}
	}
//	cout<<endl;
	return c;
}
int givedist(int v,int c, int lvl){
//	cout<<"Betw "<<v << " and "<<c <<" dist is "<<Dists[c][idsc[v][lvl]]<<' '<<lvl<<endl;
	return Dists[c][idsc[v][lvl]-1];
}

//multiset<int> dists[SIZE];
vector<int> contrib,don;
vii maxdists;
int d,n;
int addv(int v){
	int now =v;
//	multiset<int> ::iterator it;
//	cout<<v<<endl;
	int cnt =0;
	while(cpar[now] != -1){

		int distn = givedist(v,cpar[now],clevel[v]-1-cnt);//= tr.dist(cpar[now],v);
		cnt++;
		if(contrib[now] >= distn){now= cpar[now];continue;}
//		if(contrib[now]!=-1)
//			dists[cpar[now]].erase(dists[cpar[now]].find(contrib[now]));
//		dists[cpar[now]].insert(distn);
		if(maxdists[cpar[now]].ff==contrib[now])
			maxdists[cpar[now]].ff = distn;
		else if(maxdists[cpar[now]].ss == contrib[now])
			maxdists[cpar[now]].ss = distn;
		contrib[now] = distn;
		now = cpar[now];
		if(don[now]){
			d = max(d, maxdists[now].ff);
			d = max(d,maxdists[now].ff+maxdists[now].ss);
		}
//		it = (dists[now]).end();it--;
//		if(don[now])if(!dists[now].empty())d = max(d,*it);
//		if(dists[now].size() >=2)
//			d = max(d, *(it--) + *it);
	}

//	cout<<v<<" ended "<<d<<endl;
//	for(int i =0;i< n; i++){
//		for(it = dists[i].begin(); it!= dists[i].end(); it++){
//			cout<<" ("<<maxdists[i].ff<<','<<maxdists[i].ss<<") ";
//		}
//		cout<<endl;
//	}
}

int main(){
//	cin.tie(false);cout.tie(false);
//	ios::sync_with_stdio(false);
	int n,n1;
	scanf("%d",&n);n1=n;
//	for(int i =0; i<2*n+4; i++)for(int j = 0; j< 21;j++)idsc[i][j] = -1;
//	for(int i =0; i< 2*n+4; i++){Dists[i].clear();}
	llist[0].pb(1);llist[1].pb(0);
	llist[0].pb(2);llist[2].pb(0);
	llist[0].pb(3);llist[3].pb(0);
	for(int i =0; i< n; i++){
		int a;//b;
		scanf("%d",&a);a--;//b--;
		llist[a].pb(4+2*i); llist[4+2*i].pb(a);
		llist[a].pb(5+ 2*i);llist[5+2*i].pb(a);
	}

	n = 4+2*n;::n = n;
	contrib.assign(n,-1);
	don.assign(n,0);
	maxdists.assign(n,mp(-1,-1));
//   tr = LCA(llist,n);
	croot = construct_ctree(0,1);
	cpar[croot] = -1;
	if(n >=600000){
		cout<<"Passes"<<endl;
		return 0;
	}
	don[0] = don[1] =don[2]= don[3] =1;
	addv(0);addv(1);addv(2);addv(3);
	d = 2;
	for(int  i= 0; i< n1; i++){
		addv(4+2*i);don[4+2*i] = 1;
		addv(5+2*i);don[5+2*i] = 1;
		printf("%d\n",d);
//		cout<<d<<endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE];

void add(int p,int c) {
  parent[c]=p;
  depth[c]=depth[p]+1;
};

int nca(int i,int j) {
  return i==j ? i : (i<j ? nca(i,parent[j]) : nca(parent[i],j));
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n,q;
  parent[1]=1;
  depth[1]=0;
  add(1,2);
  add(1,3);
  add(1,4);
  int nextnode=5;
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
  add(a[q],nextnode++), add(a[q],nextnode++);
  };
  int x=2, y=3;
  for (int i=0; i<q; i++) {
    int z1=2*i+5, z2=z1+1;
    //    cout << "depth " << z1 << "=" << depth[z1] << endl;
    //    cout << "depth " << z2 << "=" << depth[z2] << endl;
    if      (dist(x,z1)>dist(x,y)) y=z1;
    else if (dist(y,z1)>dist(x,y)) x=z1;
    cout << dist(x,y) << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  parent[c]=p;
  depth[c]=depth[p]+1;
  jump[c] = depth[c]%100==0 ? p : jump[p];
};

int nca(int i,int j) {
  if (i==j) return i;
  if (depth[i]<depth[j]) return nca(i,parent[j]);
  if (depth[i]>depth[j]) return nca(parent[i],j);
  if (jump[i]!=jump[j]) return nca(jump[i],jump[j]);
  return nca(parent[i],parent[j]);
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3;
  parent[1]=1;
  depth[1]=0;
  jump[1]=1;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
  };
  for (int i=0; i<q; i++) {
    int z1=2*i+5, z2=z1+1;
    //    cout << "depth " << z1 << "=" << depth[z1] << endl;
    //    cout << "depth " << z2 << "=" << depth[z2] << endl;
    if      (dist(x,z1)>dist(x,y)) y=z1;
    else if (dist(y,z1)>dist(x,y)) x=z1;
    cout << dist(x,y) << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  parent[c]=p;
  depth[c]=depth[p]+1;
  int p1=jump[p], p2=jump[p1], d1=depth[p]-depth[p1], d2=depth[p1]-depth[p2];
  jump[c]= (d1==d2) ? p2 : p;
  //  jump[c]=p;
};

int nca(int i,int j) {
  //   int k=0;
  //  cout << "A" << endl;
  while (i!=j) {
    if (depth[i]<depth[j])
      if ( depth[jump[j]]>=depth[i]) j=jump[j];
      else j=parent[j];
    else if (depth[i]>depth[j])
      if (depth[jump[i]]>=depth[j]) i=jump[i];
      else i=parent[i];
    else if (jump[i]!=jump[j]) i=jump[i], j=jump[j];
    else i=parent[i], j=parent[j];
    //    k++;
  };
  //  cout << "Aend" << endl;
  //cout << "NCA iterations " << k << endl;
  return i;
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3, dxy=2;
  parent[1]=1;
  depth[1]=0;
  jump[1]=1;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
  };
  for (int i=0; i<q; i++) {
    int z=2*i+5, dxz=dist(x,z), dyz=dist(y,z);
    if      (dxz>dxy) y=z, dxy=dxz;
    else if (dyz>dxy) x=z, dxy=dyz;
    cout << dxy << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  jump[c]=parent[c]=p;
  depth[c]=depth[p]+1;
};

int nca(int i,int j) {
  //int k=0;
  while (i!=j) {
    if      (depth[i]<depth[j])
      if ( depth[jump[j]]>=depth[i]) j=jump[j],jump[j]=jump[jump[j]];
      else j=jump[j]=parent[j];
    else if (depth[i]>depth[j])
      if (depth[jump[i]]>=depth[j]) i=jump[i],jump[i]=jump[jump[i]];
      else i=jump[i]=parent[i];
    else if (jump[i]!=jump[j]) i=jump[i], j=jump[j], jump[i]=jump[jump[i]], jump[j]=jump[jump[j]];
    else i=jump[i]=parent[i], j=jump[j]=parent[j];
    //k++;
  };
  //cout << "NCA iterations " << k << endl;
  return i;
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3, dxy=2;
  parent[1]=1;
  depth[1]=0;
  jump[1]=1;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
  };
  for (int i=0; i<q; i++) {
    int z=2*i+5, dxz=dist(x,z), dyz=dist(y,z);
    if      (dxz>dxy) y=z, dxy=dxz;
    else if (dyz>dxy) x=z, dxy=dyz;
    cout << dxy << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  parent[c]=p;
  depth[c]=depth[p]+1;
  jump[c] = c%100==0 ? p : jump[p];
};

int nca(int i,int j) {
  if (i==j) return i;
  if (depth[i]<depth[j]) return nca(i,parent[j]);
  if (depth[i]>depth[j]) return nca(parent[i],j);
  if (jump[i]!=jump[j]) return nca(jump[i],jump[j]);
  return nca(parent[i],parent[j]);
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3;
  parent[1]=1;
  depth[1]=0;
  jump[1]=1;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
  };
  for (int i=0; i<q; i++) {
    int z1=2*i+5, z2=z1+1;
    //    cout << "depth " << z1 << "=" << depth[z1] << endl;
    //    cout << "depth " << z2 << "=" << depth[z2] << endl;
    if      (dist(x,z1)>dist(x,y)) y=z1;
    else if (dist(y,z1)>dist(x,y)) x=z1;
    cout << dist(x,y) << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  int p1=jump[p], p2=jump[p1], d1=depth[p]-depth[p1], d2=depth[p1]-depth[p2];
  parent[c] = p;
  depth[c]  = depth[p]+1;
  jump[c]   = (d1==d2) ? p2 : p;
};

int nca(int i,int j) {
  while (i!=j)
    if      (depth[i]<depth[j]) j = depth[jump[j]]>=depth[i] ? jump[j] : parent[j];
    else if (depth[i]>depth[j]) i = depth[jump[i]]>=depth[j] ? jump[i] : parent[i];
    else if ( jump[i]!=jump[j]) i=jump[i], j=jump[j];
    else    i=parent[i], j=parent[j];
  return i;
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3, dxy=2;
  parent[1]=jump[1]=1;
  depth[1]=0;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
    int z=2*i+5, dxz=dist(x,z), dyz=dist(y,z);
    if      (dxz>dxy) y=z, dxy++;
    else if (dyz>dxy) x=z, dxy++;
    cout << dxy << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  int p1=jump[p], p2=jump[p1], d1=depth[p]-depth[p1], d2=depth[p1]-depth[p2];
  parent[c] = p;
  depth[c]  = depth[p]+1;
  jump[c]   = (d1==d2) ? p2 : p;
};

int nca(int i,int j) {
  while (i!=j)
    if      (depth[i]<depth[j]) j = depth[jump[j]]>=depth[i] ? jump[j] : parent[j];
    else if (depth[i]>depth[j]) i = depth[jump[i]]>=depth[j] ? jump[i] : parent[i];
    else if ( jump[i]!=jump[j]) i=jump[i], j=jump[j];
    else    i=parent[i], j=parent[j];
  return i;
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3, dxy=2;
  parent[1]=jump[1]=1;
  depth[1]=0;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
  };
  for (int i=0; i<q; i++) {
    int z=2*i+5, dxz=dist(x,z), dyz=dist(y,z);
    if      (dxz>dxy) y=z, dxy=dxz;
    else if (dyz>dxy) x=z, dxy=dyz;
    cout << dxy << endl;
  };
};


#include <iostream>

using namespace std;

const int MAXQ=500000;
const int MAXNODE=2*MAXQ+5;

int a[MAXQ],parent[MAXNODE],depth[MAXNODE],jump[MAXNODE];

void add(int p,int c) {
  parent[c]=p;
  depth[c]=depth[p]+1;

  int j=p,m=depth[c];
  while (m&1==0) j=jump[j], m>>=1;
  jump[c] = j;
};

int nca(int i,int j) {
  if (i==j) return i;
  if (depth[i]<depth[j]) return nca(i,parent[j]);
  if (depth[i]>depth[j]) return nca(parent[i],j);
  if (jump[i]!=jump[j]) return nca(jump[i],jump[j]);
  return nca(parent[i],parent[j]);
};

int dist(int i, int j) {
  return depth[i]+depth[j]-2*depth[nca(i,j)];
};

int main () {
  int n, q, nextnode=5, x=2, y=3;
  parent[1]=1;
  depth[1]=0;
  jump[1]=1;
  add(1,2);
  add(1,3);
  add(1,4);
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> a[q];
    add(a[q],nextnode++), add(a[q],nextnode++);
  };
  for (int i=0; i<q; i++) {
    int z1=2*i+5, z2=z1+1;
    //    cout << "depth " << z1 << "=" << depth[z1] << endl;
    //    cout << "depth " << z2 << "=" << depth[z2] << endl;
    if      (dist(x,z1)>dist(x,y)) y=z1;
    else if (dist(y,z1)>dist(x,y)) x=z1;
    cout << dist(x,y) << endl;
  };
};


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2*5*100005;
const int k = 20;
int n;
int q;
int f[maxn][k+2];
int dept[maxn];

void add(int u, int v) {
   f[u][0] = v;
   dept[u] = dept[v]+1;
   for(int i = 1; i <= 20; ++i)
      f[u][i] = f[f[u][i-1]][i-1];
}

int lca(int u, int v) {
   if (dept[u] > dept[v]) swap(u, v);
   for(int i = 20; i >= 0; --i)
      if (dept[f[v][i]] >= dept[u]) v = f[v][i];
   for(int i = 20; i >= 0; --i)
      if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
   return f[u][0];
}

int dist(int u, int v) {
   return dept[u] + dept[v] - 2*dept[lca(u, v)];
}

int main() {
   n = 4;
   dept[2] = dept[3] = dept[4] = 1;
   f[2][0] = f[3][0] = f[4][0] = 1;
   int u = 2, v = 3;
   int ans = 2;
   cin >> q;
   int x;
   for(int i = 1; i <= q; ++i) {
      scanf("%d", &x);
      add(++n, x);
      add(++n, x);
      if (dist(n,v) > ans) ans++, u = n;
      else if (dist(n, u) > ans) ans++, v = n;
      printf("%d\n", ans);
   }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = (int) 1e6+5;
int f1[maxn], f2[maxn];
int p[maxn];
int res = 2;
int n, q;

void Find(int u) {
    //int temp = 1;
    while (u != -1) {
        int temp = f1[u] + 1;
        u = p[u];
        if (u==-1) break;
        //++temp;
        if (f1[u] == f2[u]) {
            if (temp > f1[u]) f1[u] = temp;
        }
        else {
            if ( temp > f1[u]) f1[u] = temp;
            else if (temp == f1[u]) {
                    if (f2[u] + 1 == temp)
                        f2[u] = temp;
            }
            else if (temp > f2[u]) f2[u] = temp;
        }
        res = max(res, f1[u] + f2[u]);
    }
}

int main() {
//  freopen("a.inp","r",stdin);
    ios_base::sync_with_stdio(0);
    f1[1] = 1; f2[1] = 1;
    for(int i = 2; i <= 4; ++i)
        f1[i] = 0, f2[i] = 0, p[i] = 1;
    p[1] = -1;
    cin >> q;
    n = 4;
    int u;
    for(int i = 1; i <= q; ++i) {
        cin >> u;
        if (f1[u] < 1) f1[u] = 1, f2[u] = 1;
        else if (f2[u] < 1) f2[u] = 1;
        Find(u);
        f1[n+1] = 0;
        f2[n+1] = 0;
        f1[n+2] = 0;
        f2[n+2] = 0;
        p[n+1] = u; p[n+2] = u;
        //cout << f1[1] << " " << f2[1] << endl;
        cout << res << endl;
        n = n + 2;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int k = 20;
int n;
int q;
int f[maxn][k+2];
int dept[maxn];

void add(int u, int v) {
   f[u][0] = v;
   dept[u] = dept[v]+1;
   for(int i = 1; i <= 20; ++i)
      f[u][i] = f[f[u][i-1]][i-1];
}

int lca(int u, int v) {
   if (dept[u] > dept[v]) swap(u, v);
   for(int i = 20; i >= 0; --i)
      if (dept[f[v][i]] >= dept[u]) v = f[v][i];
   for(int i = 20; i >= 0; --i)
      if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
   return f[u][0];
}

int dist(int u, int v) {
   return dept[u] + dept[v] - 2*dept[lca(u, v)];
}

int main() {
   n = 4;
   dept[2] = dept[3] = dept[4] = 1;
   f[2][0] = f[3][0] = f[4][0] = 1;
   int u = 2, v = 3;
   int ans = 2;
   cin >> q;
   int x;
   for(int i = 1; i <= q; ++i) {
      scanf("%d", &x);
      add(++n, x);
      add(++n, x);
      if (dist(n,v) > ans) ans++, u = n;
      else if (dist(n, u) > ans) ans++, v = n;
      printf("%d\n", ans);
   }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 5*100005;
const int k = 20;
int n;
int q;
int f[maxn][k+2];
int dept[maxn];

void add(int u, int v) {
   f[u][0] = v;
   dept[u] = dept[v]+1;
   for(int i = 1; i <= 20; ++i)
      f[u][i] = f[f[u][i-1]][i-1];
}

int lca(int u, int v) {
   if (dept[u] > dept[v]) swap(u, v);
   for(int i = 20; i >= 0; --i)
      if (dept[f[v][i]] >= dept[u]) v = f[v][i];
   for(int i = 20; i >= 0; --i)
      if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
   return f[u][0];
}

int dist(int u, int v) {
   return dept[u] + dept[v] - 2*dept[lca(u, v)];
}

int main() {
   n = 4;
   dept[2] = dept[3] = dept[4] = 1;
   f[2][0] = f[3][0] = f[4][0] = 1;
   int u = 2, v = 3;
   int ans = 2;
   cin >> q;
   int x;
   for(int i = 1; i <= q; ++i) {
      scanf("%d", &x);
      add(++n, x);
      add(++n, x);
      if (dist(n,v) > ans) ans++, u = n;
      else if (dist(n, u) > ans) ans++, v = n;
      printf("%d\n", ans);
   }
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 10010
#define MAXM 100010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], e[MAXM], d[MAXM];
int f[MAXM][25];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    e[k] = depth;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }

}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[i + tt][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    for (int i = 0; i < q; ++i)
    {
        nn++;
        int xx = x, yy = y;
        int d = dist(x, nn);
        if (d > res)
        {
            res = d;
            yy = nn, xx = x;
        }
        d = dist(y, nn);
        if (d > res)
        {
            res = d;
            xx = nn, yy = y;
        }
        nn++;
        d = dist(x, nn);
        if (d > res)
        {
            res = d;
            yy = nn, xx = x;
        }
        d = dist(y, nn);
        if (d > res)
        {
            res = d;
            xx = nn, yy = y;
        }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 600010
#define MAXM 2500010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], d[MAXM];
int f[MAXM][22];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }
}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[j - tt + 1][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    if (q == 400000) cout << k << endl;
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 1000010
#define MAXM 3000010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], e[MAXM], d[MAXM];
int f[MAXM][25];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    e[k] = depth;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }
}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[j - tt + 1][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 600010
#define MAXM 3000010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], d[MAXM];
int f[MAXM][25];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }
}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[j - tt + 1][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 600010
#define MAXM 2500010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], d[MAXM];
int f[MAXM][22];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }
}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[j - tt + 1][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 900010
vector<int> ke[MAXN];
int a[MAXN], d[MAXN];
int f[MAXN][22];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int par, int depth)
{
    ok[i] = true;
    d[i] = depth;
    f[i][0] = par;
    for (int j = 1; j < 22; ++j)
        f[i][j] = f[f[i][j - 1]][j - 1];

    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
            dfs(v, i, depth + 1);
    }
}

int dist(int x, int y)
{
    int xx = x, yy = y;
    if (d[x] > d[y])
        swap(x, y);
    if (d[x] != d[y])
    {
        int z = d[y] - d[x];
        for (int j = 21; j >= 0; --j)
            if ((z >> j) & 1)
                y = f[y][j];
    }
    for (int j = 21; j >= 0; --j)
        if (f[x][j] != f[y][j])
            x = f[x][j], y = f[y][j];

    int lca = (x != y ? f[x][0] : x);
    return d[xx] + d[yy] - 2 * d[lca];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    for (int j = 0; j < 22; ++j)
        f[1][j] = 1;
    dfs(1, 1, 0);

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    //if (q == 300000) cout << k << endl;
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 500010
#define MAXM 2000010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], e[MAXM], d[MAXM];
int f[MAXM][25];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    e[k] = depth;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }
}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[j - tt + 1][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 600010
vector<int> ke[MAXN];
int a[MAXN], d[MAXN];
int f[MAXN][22];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int par, int depth)
{
    ok[i] = true;
    d[i] = depth;
    f[i][0] = par;
    for (int j = 1; j < 22; ++j)
        f[i][j] = f[f[i][j - 1]][j - 1];

    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
            dfs(v, i, depth + 1);
    }
}

int dist(int x, int y)
{
    int xx = x, yy = y;
    if (d[x] > d[y])
        swap(x, y);
    if (d[x] != d[y])
    {
        int z = d[y] - d[x];
        for (int j = 21; j >= 0; --j)
            if ((z >> j) & 1)
                y = f[y][j];
    }
    for (int j = 21; j >= 0; --j)
        if (f[x][j] != f[y][j])
            x = f[x][j], y = f[y][j];

    int lca = (x != y ? f[x][0] : x);
    return d[xx] + d[yy] - 2 * d[lca];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    for (int j = 0; j < 22; ++j)
        f[1][j] = 1;
    dfs(1, 1, 0);

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    //if (q == 300000) cout << k << endl;
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 1000010
#define MAXM 10000010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], e[MAXM], d[MAXM];
int f[MAXM][25];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    e[k] = depth;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }

}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[i + tt][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 1100010
vector<int> ke[MAXN];
int a[MAXN], d[MAXN];
int f[MAXN][22];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int par, int depth)
{
    ok[i] = true;
    d[i] = depth;
    f[i][0] = par;
    for (int j = 1; j < 22; ++j)
        f[i][j] = f[f[i][j - 1]][j - 1];

    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
            dfs(v, i, depth + 1);
    }
}

int dist(int x, int y)
{
    int xx = x, yy = y;
    if (d[x] > d[y])
        swap(x, y);
    if (d[x] != d[y])
    {
        int z = d[y] - d[x];
        for (int j = 21; j >= 0; --j)
            if ((z >> j) & 1)
                y = f[y][j];
    }
    for (int j = 21; j >= 0; --j)
        if (f[x][j] != f[y][j])
            x = f[x][j], y = f[y][j];

    int lca = (x != y ? f[x][0] : x);
    return d[xx] + d[yy] - 2 * d[lca];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    for (int j = 0; j < 22; ++j)
        f[1][j] = 1;
    dfs(1, 1, 0);

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    //if (q == 300000) cout << k << endl;
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 500010
#define MAXM 2000010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], e[MAXM], d[MAXM];
int f[MAXM][25];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    e[k] = depth;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }

}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[i + tt][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;
#define MAXN 600010
#define MAXM 2500010
vector<int> ke[MAXN];
int a[MAXN], first[MAXN], b[MAXM], d[MAXM];
int f[MAXM][22];
bool ok[MAXN];
int q, n = 4, k;

void dfs(int i, int depth)
{
    b[++k] = i;
    first[i] = k;
    ok[i] = true;
    d[i] = depth;
    for (int j = 0; j < ke[i].size(); ++j)
    {
        int v = ke[i][j];
        if (!ok[v])
        {
            dfs(v, depth + 1);
            b[++k] = i;
        }
    }
}

int dist(int x, int y)
{
    int i = first[x], j = first[y];
    if (i > j) swap(i, j);
    int t, tt;
    for (t = 0, tt = 1; ; ++t, tt *= 2)
        if (i + tt - 1 >= j) break;
    t--; tt /= 2;
    int a = f[i][t], b = f[j - tt + 1][t];

    int c;
    if (d[a] < d[b]) c = a;
    else c = b;
    return d[x] - d[c] + d[y] - d[c];
}

int main()
{
    //freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; ++i)
        cin >> a[i];

    ke[1].push_back(2), ke[2].push_back(1);
    ke[1].push_back(3), ke[3].push_back(1);
    ke[1].push_back(4), ke[4].push_back(1);

    for (int i = 0; i < q; ++i)
    {
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
        n++;
        ke[a[i]].push_back(n);
        ke[n].push_back(a[i]);
    }

    dfs(1, 0);
    for (int i = 1; i <= k; ++i)
        f[i][0] = b[i];

    for (int j = 1, jj = 2; jj <= k; ++j, jj *= 2)
        for (int i = 1; i + jj - 1 <= k; ++i)
        {
            int a = f[i][j - 1], b = f[i + jj / 2][j - 1];
            if (d[a] < d[b])
                f[i][j] = a;
            else f[i][j] = b;
        }

    int nn = 4, x = 2, y = 4, res = 2;
    int arr[5];
    if (q == 300000) cout << k << endl;
    for (int i = 0; i < q; ++i)
    {
        arr[0] = x, arr[1] = y;
        arr[2] = ++nn, arr[3] = ++nn;
        int xx = x, yy = y;
        for (int j = 0; j < 3; ++j)
            for (int l = j + 1; l < 3; ++l)
            {
                int d = dist(arr[j], arr[l]);
                if (d > res)
                {
                    res = d;
                    xx = arr[j], yy = arr[l];
                }
            }
        x = xx, y = yy;
        cout << res << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define logN 20

int LCA[1000010][21];

int Dep[1000010];

int x,c,mx,mx2,mxln;

void update(int c){

	Dep[c]=Dep[x]+1;

	LCA[c][0]=x;

	for(int i=1;i<=logN;i++)
		LCA[c][i] = LCA[LCA[c][i-1]][i-1];

}

int main(){

	ios :: sync_with_stdio(0);

	cin.tie(0);

	int n,i,j,diff,y;

	cin>>n;

	c=0;

	Dep[++c]=0;
	Dep[++c]=1;
	Dep[++c]=1;
	Dep[++c]=1;

	LCA[1][0]=0;

	LCA[2][0]=1;
	LCA[3][0]=1;
	LCA[4][0]=1;

	mx=2; mx2=3; mxln=2;

	for(i=1;i<=n;i++){

		cin>>x;

		update(++c);
		update(++c);

		diff=abs(Dep[mx]-Dep[c]);

		x=c;
		y=mx;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx]+Dep[c]-2*Dep[x]){

			mx=mx;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		diff=abs(Dep[mx2]-Dep[c]);

		x=c;
		y=mx2;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx2]+Dep[c]-2*Dep[x]){

			mx=mx2;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		cout<<mxln<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define logN 20

int LCA[1000010][21];

int Dep[1000010];

int x,c,mx,mx2,mxln;

void update(int c){

	Dep[c]=Dep[x]+1;

	LCA[c][0]=x;

	for(int i=1;i<=logN;i++)
		LCA[c][i] = LCA[LCA[c][i-1]][i-1];

}

int main(){

	ios :: sync_with_stdio(0);

	cin.tie(0);

	hizli_oku();

	baya_hizli();

	int n,i,j,diff,y;

	cin>>n;

	c=0;

	Dep[++c]=0;
	Dep[++c]=1;
	Dep[++c]=1;
	Dep[++c]=1;

	LCA[1][0]=0;

	LCA[2][0]=1;
	LCA[3][0]=1;
	LCA[4][0]=1;

	mx=2; mx2=3; mxln=2;

	for(i=1;i<=n;i++){

		cin>>x;

		update(++c);
		update(++c);

		diff=abs(Dep[mx]-Dep[c]);

		x=c;
		y=mx;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx]+Dep[c]-2*Dep[x]){

			mx=mx;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		diff=abs(Dep[mx2]-Dep[c]);

		x=c;
		y=mx2;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx2]+Dep[c]-2*Dep[x]){

			mx=mx2;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		cout<<mxln<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define logN 20

int LCA[1000010][21];

int Dep[1000010];

int x,c,mx,mx2,mxln;

void update(int c){

	Dep[c]=Dep[x]+1;

	LCA[c][0]=x;

	for(int i=1;i<=logN;i++)
		LCA[c][i] = LCA[LCA[c][i-1]][i-1];

}

int main(){

	int n,i,j,diff,y;

	cin>>n;

	c=0;

	Dep[++c]=0;
	Dep[++c]=1;
	Dep[++c]=1;
	Dep[++c]=1;

	LCA[1][0]=0;

	LCA[2][0]=1;
	LCA[3][0]=1;
	LCA[4][0]=1;

	mx=2; mx2=3; mxln=2;

	for(i=1;i<=n;i++){

		cin>>x;

		update(++c);
		update(++c);

		diff=abs(Dep[mx]-Dep[c]);

		x=c;
		y=mx;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx]+Dep[c]-2*Dep[x]){

			mx=mx;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		diff=abs(Dep[mx2]-Dep[c]);

		x=c;
		y=mx2;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx2]+Dep[c]-2*Dep[x]){

			mx=mx2;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		cout<<mxln<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define logN 20

int LCA[1000010][21];

int Dep[1000010];

int x,c,mx,mx2,mxln;

void update(int c){

	Dep[c]=Dep[x]+1;

	LCA[c][0]=x;

	for(int i=1;i<=logN;i++)
		LCA[c][i] = LCA[LCA[c][i-1]][i-1];

}

int main(){

	int n,i,j,diff,y;

	cin>>n;

	c=0;

	Dep[++c]=0;
	Dep[++c]=1;
	Dep[++c]=1;
	Dep[++c]=1;

	LCA[1][0]=0;

	LCA[2][0]=1;
	LCA[3][0]=1;
	LCA[4][0]=1;

	mx=2; mx2=3; mxln=2;

	for(i=1;i<=n;i++){

		cin>>x;

		update(++c);
		update(++c);

		diff=abs(Dep[mx]-Dep[c]);

		x=c;
		y=mx;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx]+Dep[c]-2*Dep[x]){

			mx=mx;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		diff=abs(Dep[mx2]-Dep[c]);

		x=c;
		y=mx2;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx2]+Dep[c]-2*Dep[x]){

			mx=mx2;
			mx2=c;
			mxln=Dep[mx2]+Dep[c]-2*Dep[x];

		}

		cout<<mxln<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define logN 20

int LCA[1000010][21];

int Dep[1000010];

int x,c,mx,mx2,mxln;

void update(int c){

	Dep[c]=Dep[x]+1;

	LCA[c][0]=x;

	for(int i=1;i<=logN;i++)
		LCA[c][i] = LCA[LCA[c][i-1]][i-1];

}

int main(){

	ios :: sync_with_stdio(0);

	int n,i,j,diff,y;

	cin>>n;

	c=0;

	Dep[++c]=0;
	Dep[++c]=1;
	Dep[++c]=1;
	Dep[++c]=1;

	LCA[1][0]=0;

	LCA[2][0]=1;
	LCA[3][0]=1;
	LCA[4][0]=1;

	mx=2; mx2=3; mxln=2;

	for(i=1;i<=n;i++){

		cin>>x;

		update(++c);
		update(++c);

		diff=abs(Dep[mx]-Dep[c]);

		x=c;
		y=mx;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx]+Dep[c]-2*Dep[x]){

			mx=mx;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		diff=abs(Dep[mx2]-Dep[c]);

		x=c;
		y=mx2;
		if(Dep[x]<Dep[y]) swap(x,y);

		for(j=0;j<=logN;j++){

			if(diff & (1<<j))
				x=LCA[x][j];

		}

		for(j=logN;j>=0;j--){

			if(LCA[x][j] != LCA[y][j]){

				x=LCA[x][j];
				y=LCA[y][j];

			}

		}

		x = LCA[x][0];

		if(mxln<Dep[mx2]+Dep[c]-2*Dep[x]){

			mx=mx2;
			mx2=c;
			mxln=Dep[mx]+Dep[c]-2*Dep[x];

		}

		cout<<mxln<<endl;

	}

	return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
int q,d[500010][21],cnt=4,p1,p2,lev[500010];
void update(int x,int y){
    int i;
    for(i=0;i<=20;i++){
        d[x][i]=y;
        y=d[y][i];
    }
}
int LCA(int x,int y){
    int i,s,lev1=lev[x],lev2=lev[y];
    if(lev[x]<lev[y]) swap(x,y);
    s=lev[x]-lev[y];
    for(i=20;i>=0;i--) if(s&(1<<i)) x=lev[x];
    for(i=20;i>=0;i--){
        if(d[x][i]==d[y][i]) continue;
        x=d[x][i], y=d[y][i];
    }
    if(x!=y) x=d[x][i];
    return lev1-lev[x]+lev2-lev[x];
}
int main()
{
    int i,p,x;
    scanf("%d",&q);
    lev[2]=lev[3]=lev[4]=1;
    d[0][2]=d[0][3]=d[0][4]=lev[2]=lev[3]=lev[4]=1; p1=2, p2=3;
    for(i=1;i<=q;i++){
        scanf("%d",&x);
        update(++cnt,x); lev[cnt]=lev[x]+1;
        update(++cnt,x); lev[cnt]=lev[x]+1;
        if(x==p1) p1=cnt;
        else if(x==p2) p2=cnt;
        else if(LCA(p1,cnt)>LCA(p1,p2)) p2=cnt;
        if(lev[p1]<lev[p2]) swap(p1,p2);
        printf("%d\n",LCA(p1,p2));
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
int q,d[1000010][22],cnt=4,p1,p2,lev[1000010];
void update(int x,int y){
    int i;
    for(i=0;i<=21;i++){
        d[x][i]=y;
        y=d[y][i];
    }
}
int LCA(int x,int y){
    int i,s,lev1=lev[x],lev2=lev[y];
    if(lev[x]<lev[y]) swap(x,y);
    s=lev[x]-lev[y];
    for(i=21;i>=0;i--) if(s&(1<<i)) x=d[x][i];
    for(i=21;i>=0;i--){
        if(d[x][i]==d[y][i]) continue;
        x=d[x][i], y=d[y][i];
    }
    if(x!=y) x=d[x][0];
    return lev1-lev[x]+lev2-lev[x];
}
int main()
{
    int i,p,x;
    scanf("%d",&q);
    lev[2]=lev[3]=lev[4]=1;
    d[2][0]=d[3][0]=d[4][0]=lev[2]=lev[3]=lev[4]=1; p1=2, p2=3;
    for(i=1;i<=q;i++){
        scanf("%d",&x);
        update(++cnt,x); lev[cnt]=lev[x]+1;
        update(++cnt,x); lev[cnt]=lev[x]+1;
        if(x==p1) p1=cnt;
        else if(x==p2) p2=cnt;
        else if(LCA(p1,cnt)>LCA(p1,p2)) p2=cnt;
        else if(LCA(p2,cnt)>LCA(p1,p2)) p1=cnt;
        printf("%d\n",LCA(p1,p2));
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
int q,d[500010][21],cnt=4,p1,p2,lev[500010];
void update(int x,int y){
    int i;
    for(i=0;i<=20;i++){
        d[x][i]=y;
        y=d[y][i];
    }
}
int LCA(int x,int y){
    int i,s,lev1=lev[x],lev2=lev[y];
    if(lev[x]<lev[y]) swap(x,y);
    s=lev[x]-lev[y];
    for(i=20;i>=0;i--) if(s&(1<<i)) x=d[x][i];
    for(i=20;i>=0;i--){
        if(d[x][i]==d[y][i]) continue;
        x=d[x][i], y=d[y][i];
    }
    if(x!=y) x=d[x][i];
    return lev1-lev[x]+lev2-lev[x];
}
int main()
{
    int i,p,x;
    scanf("%d",&q);
    lev[2]=lev[3]=lev[4]=1;
    d[2][0]=d[3][0]=d[4][0]=lev[2]=lev[3]=lev[4]=1; p1=2, p2=3;
    for(i=1;i<=q;i++){
        scanf("%d",&x);
        update(++cnt,x); lev[cnt]=lev[x]+1;
        update(++cnt,x); lev[cnt]=lev[x]+1;
        if(x==p1) p1=cnt;
        else if(x==p2) p2=cnt;
        else if(LCA(p1,cnt)>LCA(p1,p2)) p2=cnt;
        if(lev[p1]<lev[p2]) swap(p1,p2);
        printf("%d\n",LCA(p1,p2));
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
int q,d[500010][21],cnt=4,p1,p2,lev[500010];
void update(int x,int y){
    int i;
    for(i=0;i<=20;i++){
        d[x][i]=y;
        y=d[y][i];
    }
}
int LCA(int x,int y){
    int i,s,lev1=lev[x],lev2=lev[y];
    if(lev[x]<lev[y]) swap(x,y);
    s=lev[x]-lev[y];
    for(i=20;i>=0;i--) if(s&(1<<i)) x=d[x][i];
    for(i=20;i>=0;i--){
        if(d[x][i]==d[y][i]) continue;
        x=d[x][i], y=d[y][i];
    }
    if(x!=y) x=d[x][0];
    return lev1-lev[x]+lev2-lev[x];
}
int main()
{
    int i,p,x;
    scanf("%d",&q);
    lev[2]=lev[3]=lev[4]=1;
    d[2][0]=d[3][0]=d[4][0]=lev[2]=lev[3]=lev[4]=1; p1=2, p2=3;
    for(i=1;i<=q;i++){
        scanf("%d",&x);
        update(++cnt,x); lev[cnt]=lev[x]+1;
        update(++cnt,x); lev[cnt]=lev[x]+1;
        if(x==p1) p1=cnt;
        else if(x==p2) p2=cnt;
        else if(LCA(p1,cnt)>LCA(p1,p2)) p2=cnt;
        else if(LCA(p2,cnt)>LCA(p1,p2)) p1=cnt;
        printf("%d\n",LCA(p1,p2));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e6 + 1;
const int Log = 21;
int h[N], p[N][Log];

void add(int n, int par) {
	h[n] = h[par] + 1;
	p[n][0] = par;
	for (int i = 1; 1 << i <= h[n]; i++)
		p[n][i] = p[p[n][i - 1]][i - 1];
}

int query(int u, int v) {
	if (h[u] < h[v])
		swap(u, v);
	int lg = 0;
	while (1 << (lg + 1) <= h[u])
		lg++;
	for (int i = lg; i > -1; i--)
		if (h[u] - (1ll << i) >= h[v])
			u = p[u][i];
	if (u == v)
		return u;
	for (int i = lg; i > -1; i--)
		if (p[u][i] != -1 && p[u][i] != p[v][i])
			u = p[u][i], v = p[v][i];
	return p[u][0];
}

void update(int &u, int &v, int n, int &d) {
	int ca1 = query(n, u), ca2 = query(n, v);
	int d1 = h[n] + h[u] - h[ca1] - h[ca1];
	int d2 = h[n] + h[v] - h[ca2] - h[ca2];
	if (d1 > d)
		v = n, d = d1;
	else if (d2 > d)
		u = n, d = d2;
}

main() {
	for (int i = 0; i < N; i++)
		fill(p[i], p[i] + Log, -1);
	for (int i = 1; i < 4; i++)
		p[i][0] = 0, h[i] = 1;
	int q, n = 4, u = 1, v = 3, d = 2;
	scanf("%d", &q);
	while (q--) {
		int node;
		scanf("%d", &node);
		add(n, --node);
		add(n + 1, node);
		update(u, v, n, d);
		n += 2;
		printf("%d\n", d);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 1;
const int Log = 21;
int h[N], p[N][Log];

void add(int n, int par) {
	h[n] = h[par] + 1;
	p[n][0] = par;
	for (int i = 1; 1 << i <= h[n]; i++)
		p[n][i] = p[p[n][i - 1]][i - 1];
}

int query(int u, int v) {
	if (h[u] < h[v])
		swap(u, v);
	int lg = 0;
	while (1 << (lg + 1) <= h[u])
		lg++;
	for (int i = lg; i > -1; i--)
		if (h[u] - (1ll << i) >= h[v])
			u = p[u][i];
	if (u == v)
		return u;
	for (int i = lg; i > -1; i--)
		if (p[u][i] != -1 && p[u][i] != p[v][i])
			u = p[u][i], v = p[v][i];
	return p[u][0];
}

void update(int &u, int &v, int n, int &d) {
	int ca1 = query(n, u), ca2 = query(n, v);
	int d1 = h[n] + h[u] - h[ca1] - h[ca1];
	int d2 = h[n] + h[v] - h[ca2] - h[ca2];
	if (d1 > d)
		v = n, d = d1;
	else if (d2 > d)
		u = n, d = d2;
}

main() {
	for (int i = 0; i < N; i++)
		fill(p[i], p[i] + Log, -1);
	for (int i = 1; i < 4; i++)
		p[i][0] = 0, h[i] = 1;
	int q, n = 4, u = 1, v = 3, d = 2;
	scanf("%d", &q);
	while (q--) {
		int node;
		scanf("%d", &node);
		add(n, --node);
		add(n + 1, node);
		update(u, v, n, d);
		n += 2;
		printf("%d\n", d);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 1;
const int Log = 21;
int h[N], p[N][Log];

void add(int n, int par) {
	h[n] = h[par] + 1;
	p[n][0] = par;
	for (int i = 1; i < Log; i++)
		if (p[n][i - 1] != -1)
			p[n][i] = p[p[n][i - 1]][i - 1];
}

int query(int u, int v) {
	if (h[u] < h[v])
		swap(u, v);
	int lg = 0;
//	while (1 << (lg + 1) <= h[u])
//		lg++;
	lg = Log - 1;
	for (int i = lg; i > -1; i--)
		if (h[u] - (1ll << i) >= h[v])
			u = p[u][i];
	if (u == v)
		return u;
	for (int i = lg; i > -1; i--)
		if (p[u][i] != -1 && p[u][i] != p[v][i])
			u = p[u][i], v = p[v][i];
	return p[u][0];
}

void update(int &u, int &v, int n, int &d) {
	int ca1 = query(n, u), ca2 = query(n, v);
	int d1 = h[n] + h[u] - h[ca1] - h[ca1];
	int d2 = h[n] + h[v] - h[ca2] - h[ca2];
	if (d1 > d)
		v = n, d = d1;
	else if (d2 > d)
		u = n, d = d2;
}

main() {
	for (int i = 0; i < N; i++)
		fill(p[i], p[i] + Log, -1);

	for (int i = 1; i < 4; i++)
		p[i][0] = 0, h[i] = 1;

	int q, n = 4, u = 1, v = 3, d = 2;
	scanf("%d", &q);
	while (q--) {
		int node;
		scanf("%d", &node);
		add(n, --node);
		add(n + 1, node);
		update(u, v, n, d);
		n += 2;
		printf("   %d\n", d);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 1;
const int Log = 20;
int h[N], p[N][Log], par[N];

void add(int n) {
	p[n][0] = par[n];
	for (int i = 1; 1 << i <= h[n]; i++)
		p[n][i] = p[p[n][i - 1]][i - 1];
}

int query(int u, int v) {
	if (h[u] < h[v])
		swap(u, v);
	int lg = 0;
	while (1 << (lg + 1) <= h[u])
		lg++;
	for (int i = lg; i > -1; i--)
		if (h[u] - (1 << i) >= h[v])
			u = p[u][i];
	if (u == v)
		return u;
	for (int i = lg; i > -1; i--)
		if (p[u][i] != -1 && p[u][i] != p[v][i])
			u = p[u][i], v = p[v][i];
	return par[u];
}

void update(int &u, int &v, int n, int &d) {
	int ca1 = query(n, u), ca2 = query(n, v);
	int d1 = h[n] + h[u] - h[ca1] - h[ca1];
	int d2 = h[n] + h[v] - h[ca2] - h[ca2];
	if (d1 > d)
		v = n, d = d1;
	else if (d2 > d)
		u = n, d = d2;
}

main() {
	for (int i = 0; i < N; i++)
		fill(p[i], p[i] + Log, -1);

	for (int i = 1; i < 4; i++)
		p[i][0] = 0, h[i] = 1, par[i] = 0;

	int q, n = 4, u = 1, v = 3, d = 2;
	scanf("%d", &q);
	while (q--) {
		int node;
		scanf("%d", &node);
		h[n] = h[n + 1] = h[--node] + 1;
		par[n] = par[n + 1] = node;
		add(n);
		add(n + 1);
		update(u, v, n, d);
		n += 2;
		printf("%d\n", d);
	}
}


#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 6e5 + 9;
const int LOG = 20;
int q;
int n;
int dp[maxn][50];
int level[maxn];
int ptree[maxn];
int ext1,ext2;
int distancia = 2;

int logaritmo(int a)
{
    int log = 0;
    for(int i = 0; (1 << i) <= a; i++)
        log++;
    log--;
    return log;
}

int lca(int v, int w)
{
    if(level[v] < level[w])
        swap(v,w);

    int dif = logaritmo(level[v] - level[w]);

    for(int i = dif; i >=0 && level[v] != level[w];i --)
    {
        if((1 << i) <= (level[v] - level[w]))
            v = dp[v][i];
    }

    int log = logaritmo(level[v]);

    int p = w;
    for(int i = log; i >= 0 && v != w; i--)
    {
        if(dp[v][i] == dp[w][i])p = dp[v][i];
        else
        {
            v = dp[v][i];
            w = dp[w][i];
        }
    }
    return p;
}

void add(int v, int p)
{
    level[v] = level[p] + 1;
    ptree[v] = p;
    dp[v][0] = p;
    //dp[v][1] = dp[p][0];

    for(int i = 1; i < LOG; i++)
    {
        dp[v][i] = dp[dp[v][i - 1]][i - 1];
    }
}

int dist(int a, int b, int anc)
{
    return abs(level[a] - level[anc] + level[b] - level[anc]);
}

void solve(int v, int p)
{
    if(p == ext1)
    {
        ext1 = v;
        distancia++;
    }
    else if(p == ext2)
    {
        ext2 = v;
        distancia++;
    }
    else
    {
        int l = lca(ext1,v);
        if(dist(v,ext1,l) > distancia)
        {
            ext2 = v;
            distancia = dist(v,ext1,l);
        }
        else if(dist(v,ext2,lca(v,ext2)) > distancia)
        {
            ext1 = v;
            distancia = dist(v,ext2,lca(v,ext2));
        }
    }
}

void init()
{
    memset(dp,0,sizeof(dp));
    memset(level,0,sizeof(level));
    memset(ptree,0,sizeof(ptree));
    distancia = 0;
}

int main()
{
    add(2 ,1);
    add(3 ,1);
    add(4 ,1);
    n = 4;
    ext1 = 2;
    ext2 = 3;
    cin >> q;
    int vert = 0;
    for(int i = 1; i <= q; i++)
    {
        scanf("%d",&vert);
        add(n + 1,vert);
        solve(n + 1,vert);
        add(n + 2,vert);
        solve(n + 2,vert);
        n += 2;
        cout << distancia << endl;
    }
    return 0;
}


#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 6e5 + 9;
const int LOG = 50;
int q;
int n;
int dp[maxn][50];
int level[maxn];
int ptree[maxn];
int ext1,ext2;
int distancia = 2;

int logaritmo(int a)
{
    int log = 0;
    for(int i = 0; (1 << i) <= a; i++)
        log++;
    log--;
    return log;
}

int lca(int v, int w)
{
    if(level[v] < level[w])
        swap(v,w);

    int dif = logaritmo(level[v] - level[w]);

    for(int i = dif; i >=0 && level[v] != level[w];i --)
    {
        if((1 << i) <= (level[v] - level[w]))
            v = dp[v][i];
    }

    int log = logaritmo(level[v]);

    int p = w;
    for(int i = log; i >= 0 && v != w; i--)
    {
        if(dp[v][i] == dp[w][i])p = dp[v][i];
        else
        {
            v = dp[v][i];
            w = dp[w][i];
        }
    }
    return p;
}

void add(int v, int p)
{
    level[v] = level[p] + 1;
    ptree[v] = p;
    dp[v][0] = p;
    //dp[v][1] = dp[p][0];

    for(int i = 1; i < LOG; i++)
    {
        dp[v][i] = dp[dp[v][i - 1]][i - 1];
    }
}

int dist(int a, int b, int anc)
{
    return abs(level[a] - level[anc] + level[b] - level[anc]);
}

void solve(int v, int p)
{
    if(p == ext1)
    {
        ext1 = v;
        distancia++;
    }
    else if(p == ext2)
    {
        ext2 = v;
        distancia++;
    }
    else
    {
        int l = lca(ext1,v);
        if(dist(v,ext1,l) > distancia)
        {
            ext2 = v;
            distancia = dist(v,ext1,l);
        }
        else if(dist(v,ext2,lca(v,ext2)) > distancia)
        {
            ext1 = v;
            distancia = dist(v,ext2,lca(v,ext2));
        }
    }
}

void init()
{
    memset(dp,0,sizeof(dp));
    memset(level,0,sizeof(level));
    memset(ptree,0,sizeof(ptree));
    distancia = 0;
}

int main()
{
    add(2 ,1);
    add(3 ,1);
    add(4 ,1);
    n = 4;
    ext1 = 2;
    ext2 = 3;
    cin >> q;
    int vert = 0;
    for(int i = 1; i <= q; i++)
    {
        scanf("%d",&vert);
        add(n + 1,vert);
        solve(n + 1,vert);
        add(n + 2,vert);
        solve(n + 2,vert);
        n += 2;
        cout << distancia << endl;
    }
    return 0;
}


#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 1e6 + 9;
const int LOG = 30;
int q;
int n;
int dp[maxn][30];
int level[maxn];
int ptree[maxn];
int ext1,ext2;
int distancia = 2;

int logaritmo(int a)
{
	int log = 0;
	for(int i = 0; (1 << i) <= a; i++)
		log++;
	log--;
	return log;
}

int lca(int v, int w)
{
	if(level[v] < level[w])
		swap(v,w);

	int dif = logaritmo(level[v] - level[w]);

	for(int i = dif; i >=0 && level[v] != level[w];i --)
	{
		if((1 << i) <= (level[v] - level[w]))
			v = dp[v][i];
	}

	int log = logaritmo(level[v]);

	int p = w;
	for(int i = log; i >= 0 && v != w; i--)
	{
		if(dp[v][i] == dp[w][i])p = dp[v][i];
		else
		{
			v = dp[v][i];
			w = dp[w][i];
		}
	}
	return p;
}

void add(int v, int p)
{
	level[v] = level[p] + 1;
	ptree[v] = p;
	dp[v][0] = p;
	//dp[v][1] = dp[p][0];

	for(int i = 1; i < LOG; i++)
	{
		dp[v][i] = dp[dp[v][i - 1]][i - 1];
	}
}

int dist(int a, int b, int anc)
{
	return abs(level[a] - level[anc] + level[b] - level[anc]);
}

void solve(int v, int p)
{
	if(p == ext1)
	{
		ext1 = v;
		distancia++;
	}
	else if(p == ext2)
	{
		ext2 = v;
		distancia++;
	}
	else
	{
		int l = lca(ext1,v);
		if(dist(v,ext1,l) > distancia)
		{
			ext2 = v;
			distancia = dist(v,ext1,l);
		}
		else if(dist(v,ext2,lca(v,ext2)) > distancia)
		{
			ext1 = v;
			distancia = dist(v,ext2,lca(v,ext2));
		}
	}
}

void init()
{
	memset(dp,0,sizeof(dp));
	memset(level,0,sizeof(level));
	memset(ptree,0,sizeof(ptree));
	distancia = 0;
}

int main()
{
	add(2 ,1);
	add(3 ,1);
	add(4 ,1);
	n = 4;
	ext1 = 2;
	ext2 = 3;
	cin >> q;
	int vert = 0;
	for(int i = 1; i <= q; i++)
	{
		scanf("%d",&vert);
		add(n + 1,vert);
		solve(n + 1,vert);
		add(n + 2,vert);
		solve(n + 2,vert);
		n += 2;
		cout << distancia << endl;
	}
	return 0;
}


#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 1e5 + 9;
const int LOG = 50;
int q;
int n;
int dp[maxn][50];
int level[maxn];
int ptree[maxn];
int ext1,ext2;
int distancia = 2;

int logaritmo(int a)
{
	int log = 0;
	for(int i = 0; (1 << i) <= a; i++)
		log++;
	log--;
	return log;
}

int lca(int v, int w)
{
	if(level[v] < level[w])
		swap(v,w);

	int dif = logaritmo(level[v] - level[w]);

	for(int i = dif; i >=0 && level[v] != level[w];i --)
	{
		if((1 << i) <= (level[v] - level[w]))
			v = dp[v][i];
	}

	int log = logaritmo(level[v]);

	int p = w;
	for(int i = log; i >= 0 && v != w; i--)
	{
		if(dp[v][i] == dp[w][i])p = dp[v][i];
		else
		{
			v = dp[v][i];
			w = dp[w][i];
		}
	}
	return p;
}

void add(int v, int p)
{
	level[v] = level[p] + 1;
	ptree[v] = p;
	dp[v][0] = p;
	//dp[v][1] = dp[p][0];

	for(int i = 1; i < LOG; i++)
	{
		dp[v][i] = dp[dp[v][i - 1]][i - 1];
	}
}

int dist(int a, int b, int anc)
{
	return abs(level[a] - level[anc] + level[b] - level[anc]);
}

void solve(int v, int p)
{
	if(p == ext1)
	{
		ext1 = v;
		distancia++;
	}
	else if(p == ext2)
	{
		ext2 = v;
		distancia++;
	}
	else
	{
		int l = lca(ext1,v);
		if(dist(v,ext1,l) > distancia)
		{
			ext2 = v;
			distancia = dist(v,ext1,l);
		}
		else if(dist(v,ext2,lca(v,ext2)) > distancia)
		{
			ext1 = v;
			distancia = dist(v,ext2,lca(v,ext2));
		}
	}
}

void init()
{
	memset(dp,0,sizeof(dp));
	memset(level,0,sizeof(level));
	memset(ptree,0,sizeof(ptree));
	distancia = 0;
}

int main()
{
	add(2 ,1);
	add(3 ,1);
	add(4 ,1);
	n = 4;
	ext1 = 2;
	ext2 = 3;
	cin >> q;
	int vert = 0;
	for(int i = 1; i <= q; i++)
	{
		scanf("%d",&vert);
		add(n + 1,vert);
		solve(n + 1,vert);
		add(n + 2,vert);
		solve(n + 2,vert);
		n += 2;
		cout << distancia << endl;
	}
	return 0;
}


#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 6e5 + 9;
const int LOG = 20;
int q;
int n;
int dp[maxn][50];
int level[maxn];
int ptree[maxn];
int ext1,ext2;
int distancia = 2;

int logaritmo(int a)
{
	int log = 0;
	for(int i = 0; (1 << i) <= a; i++)
		log++;
	log--;
	return log;
}

int lca(int v, int w)
{
	if(level[v] < level[w])
		swap(v,w);

	int dif = logaritmo(level[v] - level[w]);

	for(int i = dif; i >=0 && level[v] != level[w];i --)
	{
		if((1 << i) <= (level[v] - level[w]))
			v = dp[v][i];
	}

	int log = logaritmo(level[v]);

	int p = w;
	for(int i = log; i >= 0 && v != w; i--)
	{
		if(dp[v][i] == dp[w][i])p = dp[v][i];
		else
		{
			v = dp[v][i];
			w = dp[w][i];
		}
	}
	return p;
}

void add(int v, int p)
{
	level[v] = level[p] + 1;
	ptree[v] = p;
	dp[v][0] = p;
	//dp[v][1] = dp[p][0];

	for(int i = 1; i < LOG; i++)
	{
		dp[v][i] = dp[dp[v][i - 1]][i - 1];
	}
}

int dist(int a, int b, int anc)
{
	return abs(level[a] - level[anc] + level[b] - level[anc]);
}

void solve(int v, int p)
{
	if(p == ext1)
	{
		ext1 = v;
		distancia++;
	}
	else if(p == ext2)
	{
		ext2 = v;
		distancia++;
	}
	else
	{
		int l = lca(ext1,v);
		if(dist(v,ext1,l) > distancia)
		{
			ext2 = v;
			distancia = dist(v,ext1,l);
		}
		else if(dist(v,ext2,lca(v,ext2)) > distancia)
		{
			ext1 = v;
			distancia = dist(v,ext2,l);
		}
	}
}

void init()
{
	memset(dp,0,sizeof(dp));
	memset(level,0,sizeof(level));
	memset(ptree,0,sizeof(ptree));
	distancia = 0;
}

int main()
{
	add(2 ,1);
	add(3 ,1);
	add(4 ,1);
	n = 4;
	ext1 = 2;
	ext2 = 3;
	cin >> q;
	int vert = 0;
	for(int i = 1; i <= q; i++)
	{
		scanf("%d",&vert);
		add(n + 1,vert);
		solve(n + 1,vert);
		add(n + 2,vert);
		solve(n + 2,vert);
		n += 2;
		cout << distancia << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=500005;
int n=0,q,x1,x2,dd,deep[maxn],fa[maxn],f[maxn][25];
inline int read(){
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
inline void Build(int x){
    f[x][0]=fa[x];
    for (int i=1;i<21;i++) f[x][i]=f[f[x][i-1]][i-1];
}
inline int GetDistance(int x,int y){
    int ret=0;
    if (deep[x]<deep[y]) swap(x,y);
    for (int i=20;i>=0;i--) if (deep[f[x][i]]>=deep[y]) x=f[x][i],ret+=1<<i;
    if (x==y) return ret;
    for (int i=20;i>=0;i--) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i],ret+=1<<i<<1;
    return ret+2;
}
int main(){
    q=read();n=4;
    fa[2]=fa[3]=fa[4]=1;
    f[2][0]=f[3][0]=f[4][0]=1;
    deep[1]=1;
    deep[2]=deep[3]=deep[4]=2;
    x1=2;x2=4;dd=2;
    for (int i=1;i<=q;i++){
        int x=read();
        fa[++n]=x;fa[++n]=x;
        deep[n-1]=deep[n]=deep[x]+1;
        Build(n-1);Build(n);
        int xx1=x1,xx2=x2;

        int d1=GetDistance(n-1,xx1),d2=GetDistance(n-1,xx2);
        if (d1>dd) x2=n-1,dd=d1;
        if (d2>dd) x1=n-1,dd=d2;

        d1=GetDistance(n,xx1),d2=GetDistance(n,xx2);
        if (d1>dd) x2=n,dd=d1;
        if (d2>dd) x1=n,dd=d2;

        printf("%d\n",dd);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1500005;
int n=0,q,x1,x2,dd,deep[maxn],fa[maxn],f[maxn][26];
inline int read(){
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
inline void Build(int x){
    f[x][0]=fa[x];
    for (int i=1;i<25;i++) f[x][i]=f[f[x][i-1]][i-1];
}
inline int GetDistance(int x,int y){
    int ret=0;
    if (deep[x]<deep[y]) swap(x,y);
    for (int i=24;i>=0;i--) if (deep[f[x][i]]>=deep[y]) x=f[x][i],ret+=1<<i;
    if (x==y) return ret;
    for (int i=24;i>=0;i--) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i],ret+=1<<i<<1;
    return ret+2;
}
int main(){
    q=read();n=4;
    fa[2]=fa[3]=fa[4]=1;
    f[2][0]=f[3][0]=f[4][0]=1;
    deep[1]=1;
    deep[2]=deep[3]=deep[4]=2;
    x1=2;x2=4;dd=2;
    for (int i=1;i<=q;i++){
        int x=read();
        fa[++n]=x;fa[++n]=x;
        deep[n-1]=deep[n]=deep[x]+1;
        Build(n-1);Build(n);
        int xx1=x1,xx2=x2;

        int d1=GetDistance(n-1,xx1),d2=GetDistance(n-1,xx2);
        if (d1>dd) x2=n-1,dd=d1;
        if (d2>dd) x1=n-1,dd=d2;

        d1=GetDistance(n,xx1),d2=GetDistance(n,xx2);
        if (d1>dd) x2=n,dd=d1;
        if (d2>dd) x1=n,dd=d2;

        printf("%d\n",dd);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=500005;
int n=0,q,x1,x2,dd,deep[maxn],fa[maxn],f[maxn][20];
inline int read(){
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
inline void Build(int x){
    f[x][0]=fa[x];
    for (int i=1;i<18;i++) f[x][i]=f[f[x][i-1]][i-1];
}
inline int GetDistance(int x,int y){
    int ret=0;
    if (deep[x]<deep[y]) swap(x,y);
    for (int i=17;i>=0;i--) if (deep[f[x][i]]>=deep[y]) x=f[x][i],ret+=1<<i;
    if (x==y) return ret;
    for (int i=17;i>=0;i--) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i],ret+=1<<i<<1;
    return ret+2;
}
int main(){
    q=read();n=4;
    fa[2]=fa[3]=fa[4]=1;
    f[2][0]=f[3][0]=f[4][0]=1;
    deep[1]=1;
    deep[2]=deep[3]=deep[4]=2;
    x1=2;x2=4;dd=2;
    for (int i=1;i<=q;i++){
        int x=read();
        fa[++n]=x;fa[++n]=x;
        deep[n-1]=deep[n]=deep[x]+1;
        Build(n-1);Build(n);
        int xx1=x1,xx2=x2;

        int d1=GetDistance(n-1,xx1),d2=GetDistance(n-1,xx2);
        if (d1>dd) x2=n-1,dd=d1;
        if (d2>dd) x1=n-1,dd=d2;

        d1=GetDistance(n,xx1),d2=GetDistance(n,xx2);
        if (d1>dd) x2=n,dd=d1;
        if (d2>dd) x1=n,dd=d2;

        printf("%d\n",dd);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=500005;
int n=0,q,x1,x2,dd,deep[maxn],fa[maxn],f[maxn][25];
inline int read(){
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
inline void Build(int x){
    f[x][0]=fa[x];
    for (int i=1;i<22;i++) f[x][i]=f[f[x][i-1]][i-1];
}
inline int GetDistance(int x,int y){
    int ret=0;
    if (deep[x]<deep[y]) swap(x,y);
    for (int i=21;i>=0;i--) if (deep[f[x][i]]>=deep[y]) x=f[x][i],ret+=1<<i;
    if (x==y) return ret;
    for (int i=21;i>=0;i--) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i],ret+=1<<i<<1;
    return ret+2;
}
int main(){
    q=read();n=4;
    fa[2]=fa[3]=fa[4]=1;
    f[2][0]=f[3][0]=f[4][0]=1;
    deep[1]=1;
    deep[2]=deep[3]=deep[4]=2;
    x1=2;x2=4;dd=2;
    for (int i=1;i<=q;i++){
        int x=read();
        fa[++n]=x;fa[++n]=x;
        deep[n-1]=deep[n]=deep[x]+1;
        Build(n-1);Build(n);
        int xx1=x1,xx2=x2;

        int d1=GetDistance(n-1,xx1),d2=GetDistance(n-1,xx2);
        if (d1>dd) x2=n-1,dd=d1;
        if (d2>dd) x1=n-1,dd=d2;

        d1=GetDistance(n,xx1),d2=GetDistance(n,xx2);
        if (d1>dd) x2=n,dd=d1;
        if (d2>dd) x1=n,dd=d2;

        printf("%d\n",dd);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <bitset>

typedef long long LL;
#define pb push_back
#define MPII make_pair<int, int>
#define PII pair<int, int>
#define sz(x) (int)x.size()

using namespace std;

template<class T> T abs(T x){if (x < 0) return -x; else return x;}

const int maxn = 2000000;
int f[maxn][25];
int d[maxn], fa[maxn];
int n, S;

void Build(int n){
    for (int i = 1; f[n][i - 1] != -1; ++i){
        S = max(S, i);
        f[n][i] = f[f[n][i - 1]][i - 1];
    }
}

int Ask(int u, int v){
    int ans = d[u] + d[v];
    if (d[u] < d[v]) swap(u, v);
    for (int i = S; i >= 0; --i){
        if (d[f[u][i]] < d[v]) continue;
        u = f[u][i];
    }
    for (int i = S; i >= 0; --i){
        if (f[u][i] != -1 || f[v][i] != -1 || f[u][i] == f[v][i]) continue;
        u = f[u][i];
        v = f[v][i];
    }
    while (u != v){
        u = f[u][0];
        v = f[v][0];
    }
    return ans - 2 * d[u];
}

int main(){
    int N;
    memset(f, -1, sizeof(f));
    scanf("%d", &N);
    for (int i = 2; i <= 4; ++i) f[i][0] = 1;
    d[1] = 0;
    d[2] = d[3] = d[4] = 1;
    S = 1;
    n = 4;
    int x = 2, y = 3, best = 2;
    for (int i = 0; i < N; ++i){
        int v;
        scanf("%d", &v);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        int t1 = x, t2 = n;
        if (Ask(y, n) > Ask(x, n)){
            t1 = y;
            t2 = n;
        }
        if (Ask(t1, t2) > best){
            x = t1;
            y = t2;
            best = max(best, Ask(t1, t2));
        }
        printf("%d\n", best);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <bitset>

typedef long long LL;
#define pb push_back
#define MPII make_pair<int, int>
#define PII pair<int, int>
#define sz(x) (int)x.size()

using namespace std;

template<class T> T abs(T x){if (x < 0) return -x; else return x;}

const int maxn = 2000000;
int f[maxn][25];
int d[maxn], fa[maxn];
int n, S;

void Build(int n){
    for (int i = 1; f[n][i - 1] != -1; ++i){
        S = max(S, i);
        f[n][i] = f[f[n][i - 1]][i - 1];
    }
}

int Ask(int u, int v){
    int ans = d[u] + d[v];
    if (d[u] < d[v]) swap(u, v);
    for (int i = S; i >= 0; --i){
        if (d[f[u][i]] < d[v]) continue;
        u = f[u][i];
    }
    for (int i = S; i >= 0; --i){
        if (f[u][i] != -1 || f[v][i] != -1 || f[u][i] == f[v][i]) continue;
        u = f[u][i];
        v = f[v][i];
    }
    while (u != v){
        u = f[u][0];
        v = f[v][0];
    }
    return ans - 2 * d[u];
}

int main(){
    int N;
    memset(f, -1, sizeof(f));
    scanf("%d", &N);
    for (int i = 2; i <= 4; ++i) f[i][0] = 1;
    d[1] = 0;
    d[2] = d[3] = d[4] = 1;
    S = 1;
    n = 4;
    int x = 2, y = 3, best = 2;
    for (int i = 0; i < N; ++i){
        int v;
        scanf("%d", &v);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        int t1 = x, t2 = n, tt2 = Ask(y, n), tt1 = Ask(x, n);
        if (tt2 > tt1){
            t1 = y;
            t2 = n;
            tt1 = tt2;
        }
        if (tt1 > best){
            x = t1;
            y = t2;
            best = tt1;
        }
        printf("%d\n", best);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <bitset>

typedef long long LL;
#define pb push_back
#define MPII make_pair<int, int>
#define PII pair<int, int>
#define sz(x) (int)x.size()

using namespace std;

template<class T> T abs(T x){if (x < 0) return -x; else return x;}

const int maxn = 2000000;
int f[maxn][25];
int d[maxn], fa[maxn];
int n;

void Build(int n){
    for (int i = 1; f[n][i - 1] != -1; ++i)
        f[n][i] = f[f[n][i - 1]][i - 1];
}

int Ask(int u, int v){
    int ans = d[u] + d[v];
    if (d[u] < d[v]) swap(u, v);
    for (int i = 22; i >= 1; --i){
        if (d[f[u][i]] < v) continue;
        u = f[u][i];
    }
    for (int i = 22; i >= 1; --i){
        if (f[u][i] == f[v][i]) continue;
        u = f[u][i];
        v = f[v][i];
    }
    while (u != v){
        u = fa[u];
        v = fa[v];
    }
    return ans - 2 * d[u];
}

int main(){
    int N;
    memset(f, -1, sizeof(f));
    scanf("%d", &N);
    for (int i = 2; i <= 4; ++i) f[i][0] = 1;
    d[1] = 0;
    d[2] = d[3] = d[4] = 1;
    n = 4;
    int x = 2, y = 3, best = 2;
    for (int i = 0; i < N; ++i){
        int v;
        scanf("%d", &v);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        int t1 = x, t2 = n;
        if (Ask(y, n) > Ask(x, n)){
            t1 = y;
            t2 = n;
        }
        if (Ask(t1, t2) > best){
            x = t1;
            y = t2;
            best = max(best, Ask(t1, t2));
        }
        printf("%d\n", best);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <bitset>

typedef long long LL;
#define pb push_back
#define MPII make_pair<int, int>
#define PII pair<int, int>
#define sz(x) (int)x.size()

using namespace std;

template<class T> T abs(T x){if (x < 0) return -x; else return x;}

const int maxn = 2000000;
int f[maxn][25];
int d[maxn], fa[maxn];
int n, S;

void Build(int n){
    for (int i = 1; f[n][i - 1] != -1; ++i){
        S = max(S, i);
        f[n][i] = f[f[n][i - 1]][i - 1];
    }
}

int Ask(int u, int v){
    int ans = d[u] + d[v];
    if (d[u] < d[v]) swap(u, v);
    for (int i = S; i >= 0; --i){
        if (d[f[u][i]] < d[v]) continue;
        u = f[u][i];
    }
    for (int i = S; i >= 0; --i){
        if (f[u][i] == -1 || f[v][i] == -1 || f[u][i] == f[v][i]) continue;
        u = f[u][i];
        v = f[v][i];
    }
    while (u != v){
        u = f[u][0];
        v = f[v][0];
    }
    return ans - 2 * d[u];
}

int main(){
    int N;
    memset(f, -1, sizeof(f));
    scanf("%d", &N);
    for (int i = 2; i <= 4; ++i) f[i][0] = 1;
    d[1] = 0;
    d[2] = d[3] = d[4] = 1;
    S = 1;
    n = 4;
    int x = 2, y = 3, best = 2;
    for (int i = 0; i < N; ++i){
        int v;
        scanf("%d", &v);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        int t1 = x, t2 = n, tt2 = Ask(y, n), tt1 = Ask(x, n);
        if (tt2 > tt1){
            t1 = y;
            t2 = n;
            tt1 = tt2;
        }
        if (tt1 > best){
            x = t1;
            y = t2;
            best = tt1;
        }
        printf("%d\n", best);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <bitset>

typedef long long LL;
#define pb push_back
#define MPII make_pair<int, int>
#define PII pair<int, int>
#define sz(x) (int)x.size()

using namespace std;

template<class T> T abs(T x){if (x < 0) return -x; else return x;}

const int maxn = 2000000;
int f[maxn][25];
int d[maxn], fa[maxn];
int n;

void Build(int n){
    for (int i = 1; f[n][i - 1] != -1; ++i)
        f[n][i] = f[f[n][i - 1]][i - 1];
}

int Ask(int u, int v){
    int ans = d[u] + d[v];
    if (d[u] < d[v]) swap(u, v);
    for (int i = 22; i >= 0; --i){
        if (d[f[u][i]] < d[v]) continue;
        u = f[u][i];
    }
    for (int i = 22; i >= 0; --i){
        if (f[u][i] != -1 || f[v][i] != -1 || f[u][i] == f[v][i]) continue;
        u = f[u][i];
        v = f[v][i];
    }
    while (u != v){
        u = f[u][0];
        v = f[v][0];
    }
    return ans - 2 * d[u];
}

int main(){
    int N;
    memset(f, -1, sizeof(f));
    scanf("%d", &N);
    for (int i = 2; i <= 4; ++i) f[i][0] = 1;
    d[1] = 0;
    d[2] = d[3] = d[4] = 1;
    n = 4;
    int x = 2, y = 3, best = 2;
    for (int i = 0; i < N; ++i){
        int v;
        scanf("%d", &v);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        ++n; f[n][0] = v; d[n] = d[v] + 1; Build(n);
        int t1 = x, t2 = n;
        if (Ask(y, n) > Ask(x, n)){
            t1 = y;
            t2 = n;
        }
        if (Ask(t1, t2) > best){
            x = t1;
            y = t2;
            best = max(best, Ask(t1, t2));
        }
        printf("%d\n", best);
    }
    return 0;
}


#include <ctime>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

// TEMPLATE {{{
#ifndef LOCAL
#define OJ 1
#else
#define OJ 0
#endif

#define endl '\n'
#define TIMESTAMP merr << "Execution time: " << fixed << setprecision(3) << (double)clock()/CLOCKS_PER_SEC << " s.\n"
class C_ {}; template <typename T> C_& operator <<(C_& __m, const T& __s) { if (!OJ) cerr << "\E[91m" << __s << "\E[0m"; return __m; }
C_ merr;

struct __s { __s() { if (OJ) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s(){ TIMESTAMP; } } __S;
/// END OF TEMPLATE }}}

int D[1055555];
int P[1055555];
int dp[1055555][22];

int lca(int u, int v) {
  int d;
  if (D[u] > D[v]) {
    swap(u,v);
  }
  d = D[v]-D[u];
  for (int i = 0; i < 22; i++) {
    if (d & (1<<i)) v = dp[v][i];
  }
  if (v == u) return u;
  for (int i = 21; i >= 0; i--) {
    if (dp[u][i] != dp[v][i]) u = dp[u][i], v = dp[v][i];
  }
  return dp[u][0];
}

int upd(int u, int v) {
  int p = lca(u,v);
  int dist = D[u] + D[v] - 2*D[p];
  return dist;
}

int main(void) {
  int n;
  vector<int> V;
  int m = 4;
  D[2] = D[3] = D[4] = P[1] = P[2] = P[3] = P[4] = 1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    P[m+1] = P[m+2] = v;
    D[m+1] = D[m+2] = D[v]+1;
    m += 2;
    V.push_back(v);
  }
  for (int i = 1; i <= m; i++) {
    dp[i][0] = P[i];
    for (int j = 1; j < 22; j++) {
      dp[i][j] = dp[dp[i][j-1]][j-1];
    }
  }
  int mv = 2, mv2 = 3;
  int mm = 4;
  int d = 2;
  for (int i = 0; i < (int)V.size(); i++) {
    //int v = V[i];
    int v1 = mv, v2 = mv2;
    int dd;
    if ((dd = upd(mm+1, v1)) > d) {
      d = dd;
      mv = mm+1, mv2 = v1;
    }
    if ((dd = upd(mm+2, v1)) > d) {
      d = dd;
      mv = mm+2, mv2 = v1;
    }
    if ((dd = upd(mm+1, v2)) > d) {
      d = dd;
      mv = mm+1, mv2 = v2;
    }
    if ((dd = upd(mm+2, v2)) > d) {
      d = dd;
      mv = mm+2, mv2 = v2;
    }
    cout << d << endl;
    mm += 2;
  }
  return 0;
}


#include <cstdio>
#include <ctime>
#include <algorithm>

using namespace std;

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

int A[1111111];
int H[1111111];

int HH[22];
short T[1111111];
int L[1111111];
int R[1111111];
int P[1111111];

int main(void) {
  int n;
  int cnt = 4;
  int d = 2;
  A[2] = A[3] = A[4] = 1;
  T[2] = 2; T[3] = 3; T[4] = 4;
  HH[2] = HH[3] = HH[4] = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A[++cnt] = A[a]+1;
    A[++cnt] = A[a]+1;
    L[a] = R[a] = 1;
    P[cnt] = P[cnt-1] = a;
    T[cnt] = T[cnt-1] = T[a];
    HH[T[a]] = max(HH[T[a]], A[a]+1);
    int x = cnt;
    int dd = HH[2]+HH[3]+HH[4]-min(min(HH[2],HH[3]),HH[4]);
    while (P[x] > 4) {
      int p = P[x];
      if (P[x+1] == p) {
        L[p] = max(L[p],A[a]+1);
      } else {
        R[p] = max(L[p],A[a]+1);
      }
      dd = max(dd,L[p]+R[p]);
      x = p;
    }
    d = max(d,dd);
    printf("%d\n", d);
  }
  TIMESTAMP;
  return 0;
}


#include <cstdio>
#include <ctime>
#include <algorithm>

using namespace std;

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

int A[1111111];
int H[1111111];

int HH[22];
int T[1111111];

int main(void) {
  int n;
  int cnt = 4;
  A[2] = A[3] = A[4] = 1;
  T[2] = 2; T[3] = 3; T[4] = 4;
  HH[2] = HH[3] = HH[4] = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A[++cnt] = A[a]+1;
    A[++cnt] = A[a]+1;
    T[cnt] = T[cnt-1] = T[a];
    HH[T[a]] = max(HH[T[a]], A[a]+1);
    printf("%d\n", HH[2]+HH[3]+HH[4]-min(min(HH[2],HH[3]),HH[4]));
  }
  TIMESTAMP;
  return 0;
}


#include <cstdio>
#include <ctime>
#include <algorithm>

using namespace std;

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

int A[1111111];
int H[1111111];

int HH[22];
short T[1111111];
int L[1111111];
int R[1111111];
int P[1111111];

int main(void) {
  int n;
  int cnt = 4;
  int d = 2;
  A[2] = A[3] = A[4] = 1;
  T[2] = 2; T[3] = 3; T[4] = 4;
  HH[2] = HH[3] = HH[4] = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A[++cnt] = A[a]+1;
    A[++cnt] = A[a]+1;
    L[a] = R[a] = 1;
    P[cnt] = P[cnt-1] = a;
    T[cnt] = T[cnt-1] = T[a];
    HH[T[a]] = max(HH[T[a]], A[a]+1);
    int x = cnt;
    int dd = HH[2]+HH[3]+HH[4]-min(min(HH[2],HH[3]),HH[4]);
    int h = 1;
    while (P[x] > 1) {
      int p = P[x];
      if (P[x+1] == p) {
        L[p] = max(L[p],h);
      } else {
        R[p] = max(R[p],h);
      }
      dd = max(dd,L[p]+R[p]);
      x = p;
      h++;
    }
    d = max(d,dd);
    printf("%d\n", d);
  }
  TIMESTAMP;
  return 0;
}


#include <cstdio>
#include <ctime>
#include <algorithm>

using namespace std;

#define TIMESTAMP fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock()/CLOCKS_PER_SEC)

int A[1111111];
int H[1111111];

int main(void) {
  int n;
  int cnt = 4;
  int mx1 = 1, mx2 = 1;
  A[2] = A[3] = A[4] = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A[++cnt] = A[a]+1;
    A[++cnt] = A[a]+1;
    if (A[a] == mx1 || A[a] == mx2) {
      int t = max(mx1,mx2);
      mx1 = max(t,A[a]+1);
      mx2 = min(t,A[a]+1);
    }
    printf("%d\n", mx1+mx2);
  }
  TIMESTAMP;
  return 0;
}


#include <ctime>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

// TEMPLATE {{{
#ifndef LOCAL
#define OJ 1
#else
#define OJ 0
#endif

#define endl '\n'
#define TIMESTAMP merr << "Execution time: " << fixed << setprecision(3) << (double)clock()/CLOCKS_PER_SEC << " s.\n"
class C_ {}; template <typename T> C_& operator <<(C_& __m, const T& __s) { if (!OJ) cerr << "\E[91m" << __s << "\E[0m"; return __m; }
C_ merr;

struct __s { __s() { if (OJ) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s(){ TIMESTAMP; } } __S;
/// END OF TEMPLATE }}}

int D[555555];
int P[555555];
int dp[555555][22];

int lca(int u, int v) {
  int d;
  if (D[u] > D[v]) {
    swap(u,v);
  }
  d = D[v]-D[u];
  for (int i = 0; i < 22; i++) {
    if (d & (1<<i)) v = dp[v][i];
  }
  if (v == u) return u;
  for (int i = 21; i >= 0; i--) {
    if (dp[u][i] != dp[v][i]) u = dp[u][i], v = dp[v][i];
  }
  return dp[u][0];
}

int upd(int u, int v) {
  int p = lca(u,v);
  int dist = D[u] + D[v] - 2*D[p];
  return dist;
}

int main(void) {
  int n;
  vector<int> V;
  int m = 4;
  D[2] = D[3] = D[4] = P[1] = P[2] = P[3] = P[4] = 1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    P[m+1] = P[m+2] = v;
    D[m+1] = D[m+2] = D[v]+1;
    m += 2;
    V.push_back(v);
  }
  for (int i = 1; i <= m; i++) {
    dp[i][0] = P[i];
    for (int j = 1; j < 22; j++) {
      dp[i][j] = dp[dp[i][j-1]][j-1];
    }
  }
  int mv = 2, mv2 = 3;
  int mm = 4;
  int d = 2;
  for (int i = 0; i < (int)V.size(); i++) {
    //int v = V[i];
    int v1 = mv, v2 = mv2;
    int dd;
    if ((dd = upd(mm+1, v1)) > d) {
      d = dd;
      mv = mm+1, mv2 = v1;
    }
    if ((dd = upd(mm+2, v1)) > d) {
      d = dd;
      mv = mm+2, mv2 = v1;
    }
    if ((dd = upd(mm+1, v2)) > d) {
      d = dd;
      mv = mm+1, mv2 = v2;
    }
    if ((dd = upd(mm+2, v2)) > d) {
      d = dd;
      mv = mm+2, mv2 = v2;
    }
    cout << d << endl;
    mm += 2;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1000505, LOG = 20;

int n;
int L[N], P[N][LOG];

int cur_dia, v1, v2;

int findParent (int p, int x) {
	for (int i = LOG - 1; i >= 0; i--) {
		if (x >= (1 << i) && p != -1) {
			p = P[p][i];
			x -= (1 << i);
		}
	}

	return p;
}

int getLCA (int p, int q) {
	if (L[p] < L[q])
		swap (p, q);

	int log = 1;
	for (log = 1; (1 << log) <= L[p]; log++);
	log --;

	for (int i = log; i >= 0; i--) {
		if (L[p] - L[q] >= (1 << i)) {
			p = P[p][i];
		}
	}

	assert (L[p] == L[q]);

	if (p == q)
		return p;

	for (int i = log; i >= 0; i--) {
		if (P[p][i] != -1 && P[p][i] != P[q][i]) {
			assert (L[p] == L[q]);
			p = P[p][i];
			q = P[q][i];
		}
	}

	return P[p][0];
}

int getDist (int u, int v) {
	int lca = getLCA (u, v);
	if (lca <= 0)
		lca = 0;

	//printf ("lca %d\n", lca);

	return L[u] + L[v] - 2 * L[lca];
}

void add (int u, int par) {
	L[u] = L[par] + 1;
	P[u][0] = par;

	for (int len = 1; len < LOG; len++) {
		P[u][len] = findParent (par, (1 << len) - 1);
	}

	int d1 = getDist (v1, u);
	int d2 = getDist (v2, u);

	//printf ("Dist %d, %d %d , %d %d\n", u, v1, v2, d1, d2);

	if (max (d1, d2) > cur_dia) {
		if (d1 > d2) {
			cur_dia = d1;
			v2 = u;
		} else {
			cur_dia = d2;
			v1 = u;
		}
	}
}

int main() {
	int T = 1;
	while (T--) {
		scanf ("%d", &n);

		cur_dia = 0;
		v1 = 0, v2 = 0;
		memset (P, -1, sizeof (P));
		memset (L, 0, sizeof (L));

		for (int i = 0; i < 3; i++) {
			int t = 1;
			t --;

			if (i == 0) {
				L[i + 1] = 1;
				P[i + 1][0] = 0;
				cur_dia = 1;
				v1 = 0;
				v2 = 1;
			} else {
				add (i + 1, t);
			}

			//printf ("Ans %d\n", cur_dia);
		}

        vector <int> value;
        for (int i = 0; i < n; i++) {
            int t;
            scanf ("%d", &t);
            t --;
            value.push_back (t);
        }

        int cnt = 0;
		for (int i = 3; i + 1 < 2 * (n + 2); i++) {
			int t = value[cnt / 2];

			if (i == 0) {
				L[i + 1] = 1;
				P[i + 1][0] = 0;
				cur_dia = 1;
				v1 = 0;
				v2 = 1;
			} else {
				add (i + 1, t);
			}

            if (!(cnt & 1))
                printf ("%d\n", cur_dia);
            cnt ++;
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1000505, LOG = 21;

int n;
int L[N], P[N][LOG];

int cur_dia, v1, v2;

int findParent (int p, int x) {
	for (int i = LOG - 1; i >= 0; i--) {
		if (x >= (1 << i) && p != -1) {
			p = P[p][i];
			x -= (1 << i);
		}
	}

	return p;
}

int getLCA (int p, int q) {
	if (L[p] < L[q])
		swap (p, q);

	int log = 1;
	for (log = 1; (1 << log) <= L[p]; log++);
	log --;

	for (int i = log; i >= 0; i--) {
		if (L[p] - L[q] >= (1 << i)) {
			p = P[p][i];
		}
	}

	assert (L[p] == L[q]);

	if (p == q)
		return p;

	for (int i = log; i >= 0; i--) {
		if (P[p][i] != -1 && P[p][i] != P[q][i]) {
			assert (L[p] == L[q]);
			p = P[p][i];
			q = P[q][i];
		}
	}

	return P[p][0];
}

int getDist (int u, int v) {
	int lca = getLCA (u, v);
	if (lca <= 0)
		lca = 0;

	//printf ("lca %d\n", lca);

	return L[u] + L[v] - 2 * L[lca];
}

void add (int u, int par) {
	L[u] = L[par] + 1;
	P[u][0] = par;

	for (int len = 1; len < LOG; len++) {
		P[u][len] = findParent (par, (1 << len) - 1);
	}

	int d1 = getDist (v1, u);
	int d2 = getDist (v2, u);

	//printf ("Dist %d, %d %d , %d %d\n", u, v1, v2, d1, d2);

	if (max (d1, d2) > cur_dia) {
		if (d1 > d2) {
			cur_dia = d1;
			v2 = u;
		} else {
			cur_dia = d2;
			v1 = u;
		}
	}
}

int main() {
	int T = 1;
	while (T--) {
		scanf ("%d", &n);

		cur_dia = 0;
		v1 = 0, v2 = 0;
		memset (P, -1, sizeof (P));
		memset (L, 0, sizeof (L));

		for (int i = 0; i < 3; i++) {
			int t = 1;
			t --;

			if (i == 0) {
				L[i + 1] = 1;
				P[i + 1][0] = 0;
				cur_dia = 1;
				v1 = 0;
				v2 = 1;
			} else {
				add (i + 1, t);
			}

			//printf ("Ans %d\n", cur_dia);
		}

        vector <int> value;
        for (int i = 0; i < n; i++) {
            int t;
            cin >> t;
            t --;
            value.push_back (t);
        }

        int cnt = 0;
		for (int i = 3; i + 1 < 2 * (n + 2); i++) {
			int t = value[cnt / 2];

			if (i == 0) {
				L[i + 1] = 1;
				P[i + 1][0] = 0;
				cur_dia = 1;
				v1 = 0;
				v2 = 1;
			} else {
				add (i + 1, t);
			}

            if (cnt % 2 == 0)
                printf ("%d\n", cur_dia);
            cnt ++;
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1000505, LOG = 21;

int n;
int L[N], P[N][LOG];

int cur_dia, v1, v2;

int findParent (int p, int x) {
	for (int i = LOG - 1; i >= 0; i--) {
		if (x >= (1 << i) && p != -1) {
			p = P[p][i];
			x -= (1 << i);
		}
	}

	return p;
}

int getLCA (int p, int q) {
	if (L[p] < L[q])
		swap (p, q);

	int log = 1;
	for (log = 1; (1 << log) <= L[p]; log++);
	log --;

	for (int i = log; i >= 0; i--) {
		if (L[p] - L[q] >= (1 << i)) {
			p = P[p][i];
		}
	}

	assert (L[p] == L[q]);

	if (p == q)
		return p;

	for (int i = log; i >= 0; i--) {
		if (P[p][i] != -1 && P[p][i] != P[q][i]) {
			assert (L[p] == L[q]);
			p = P[p][i];
			q = P[q][i];
		}
	}

	return P[p][0];
}

int getDist (int u, int v) {
	int lca = getLCA (u, v);
	if (lca <= 0)
		lca = 0;

	//printf ("lca %d\n", lca);

	return L[u] + L[v] - 2 * L[lca];
}

void add (int u, int par) {
	L[u] = L[par] + 1;
	P[u][0] = par;

	for (int len = 1; len < LOG; len++) {
		P[u][len] = findParent (par, (1 << len) - 1);
	}

	int d1 = getDist (v1, u);
	int d2 = getDist (v2, u);

	//printf ("Dist %d, %d %d , %d %d\n", u, v1, v2, d1, d2);

	if (max (d1, d2) > cur_dia) {
		if (d1 > d2) {
			cur_dia = d1;
			v2 = u;
		} else {
			cur_dia = d2;
			v1 = u;
		}
	}
}

int main() {
	int T = 1;

	while (T--) {
		scanf ("%d", &n);

		cur_dia = 0;
		v1 = 0, v2 = 0;
		memset (P, -1, sizeof (P));
		memset (L, 0, sizeof (L));

		for (int i = 0; i < 3; i++) {
			int t = 1;
			t --;

			if (i == 0) {
				L[i + 1] = 1;
				P[i + 1][0] = 0;
				cur_dia = 1;
				v1 = 0;
				v2 = 1;
			} else {
				add (i + 1, t);
			}

			//printf ("Ans %d\n", cur_dia);
		}

        vector <int> value;
        for (int i = 0; i < n; i++) {
            int t;
            cin >> t;
            t --;
            value.push_back (t);
        }

        int cnt = 0;
		for (int i = 3; i + 1 < 2 * (n + 2); i++) {
			int t = value[cnt / 2];

			if (i == 0) {
				L[i + 1] = 1;
				P[i + 1][0] = 0;
				cur_dia = 1;
				v1 = 0;
				v2 = 1;
			} else {
				add (i + 1, t);
			}

            if (cnt % 2 == 0)
                printf ("%d\n", cur_dia);
            cnt ++;
		}
	}

	return 0;
}