#include <stdio.h>

int main()
{
    int N,n,d;

    while(1)
    {
        scanf("%d",&N);

        if(N==0)
            break;

        int i;

       int dd[210],nn[210];
        dd[0] = 0; nn[0] = 0;
        

        for(i=1;i<N;i++)
        {
            scanf("%d %d",&n,&d);

            if(d==0)       
            {
                dd[i] = dd[n] +1;
                nn[i] = nn[n];
            }
            else if(d==1)       
            {
                dd[i] = dd[n];
                nn[i] = nn[n]+1;
            }
            else if(d==2)       
            {
                dd[i] = dd[n]-1;
                nn[i] = nn[n];
            }
            else if(d==3)       
            {
                dd[i] = dd[n];
                nn[i] = nn[n]-1;
            }
        }
    int a = dd[0],b=dd[0],c=nn[0],d=nn[0];
    for(i=0;i<=N-1;i++)
        {
            if(dd[a] < dd[i])
                a = i;
            
            if(dd[b] > dd[i])
                b = i;

            if(nn[c] < nn[i])
                c = i;

            if(nn[d] > nn[i])
                d = i;
        }

    printf("%d %d\n",dd[a]-dd[b]+1,nn[c]-nn[d]+1);
        
    }


    return 0;
}
