#include <iostream>

using namespace std;

int main(void) {

        int ln = 1, num;
        int n, d;
        int x, y;
        int width_min, width_max;
        int height_min, height_max;

        while(ln != 0) {
                cin >> ln;

                if(ln == 0) {
                        break;
                } else if(ln == 1) {
                    cout << "0 0" << endl;
                } else {

                        int** position = new int*[ln*ln];
                        for(int l = 0; l < ln*ln; l++) {
                            position[l] = new int[2];
                        }
                        num = 0;
                        x = y = 0;
                        position[num][1] = x;
                        position[num][2] = y;
                        num++;
                        width_min = width_max = 0;
                        height_min = height_max = 0;

                        for(int i = ln-1; i > 0; i--) {
                                cin >> n >> d;

                                //???????????´?????????????????????
                                x = position[n][0];
                                y = position[n][1];

                                //??¨?§?????????´???????¨????
                                if(d == 0) {
                                        x--;
                                        position[num][0] = x;
                                        position[num][1] = y;
                                        num++;
                                        if(x < width_min) width_min = x;
                                }
                                else if(d == 1) {
                                        y--;
                                        position[num][0] = x;
                                        position[num][1] = y;
                                        num++;
                                        if(y < height_min) height_min = y;
                                }
                                else if(d == 2) {
                                        x++;
                                        position[num][0] = x;
                                        position[num][1] = y;
                                        num++;
                                        if(x > width_max) width_max = x;
                                }
                                else if(d == 3) {
                                        y++;
                                        position[num][0] = x;
                                        position[num][1] = y;
                                        num++;
                                        if(y > height_max) height_max = y;
                                }
                                /*
                                cout << "height_max = " << height_max << endl;
                                cout << "height_min = " << height_min << endl;
                                cout << "width_max = " << width_max << endl;
                                cout << "width_min = " << width_min << endl;
                                */
                        }

                        x = width_max - width_min + 1;
                        y = height_max - height_min + 1;
                        cout << x << " " << y << endl;
                }
        }
}

Measured memory_bytes: 34292