Pablo Squarson's Headache

No attempts yetTime limit1sMemory limit128 MB

Problem

Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares".

In the center of his studio there is a huge table, and beside it are many squares, all of the same size. Pablo first places one square on the table. Then he places the remaining squares one by one, always attaching each new square flush against one side of a square that is already on the table, with machine-like precision. No square is ever placed on top of another.

When a piece is finished, Pablo wants to know the size of the box that would fit it, that is, the width and the height of the artwork. Given a record of how Pablo built a piece, write a program that computes its width and height. The side length of each square is 1.

Input

The input consists of several datasets. Each dataset describes how Pablo built one piece of artwork and has the following format.

N
n1 d1
n2 d2
...
nN-1 dN-1

The first line contains the number of squares $N$ used to make the piece. $N$ is a positive integer smaller than $200$.

The remaining $N-1$ lines are square placement instructions. The squares are numbered in the order they are placed: the first square is numbered $0$, and the squares placed after it are numbered $1, 2, \dots, N-1$. No placement instruction is given for square $0$.

The $i$-th instruction "$n_i\ d_i$" means that square $i$ ($1 \le i \le N-1$) is placed next to square $n_i$ in the direction $d_i$, where $d_i$ is $0$ for left, $1$ for down, $2$ for right, and $3$ for up.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output one line containing the width and the height of the piece of artwork, separated by a single space. Each line must not contain any other characters.