Description

F, who likes to dance, decided to practice a hardcore dance called JUMP STYLE at a certain dance hall.

The floor is tiled in an N × N grid. To support the dance practice, each tile has the coordinates of the tile to jump to the next step. F has strong motor nerves through daily practice and can jump to any tile on the floor.

F realized that if he continued this dance for a long enough time, he would eventually enter a steady state and just loop on the same route. F wondered how many such loops existed on this floor and decided to talk to you, the programmer.



Input

The input consists of multiple test cases.

Each test case begins with one line containing the length N of one side of the floor. (1 ≤ N ≤ 100)

The following N lines represent the coordinates of the jump destination written on each tile on the floor as follows.


\\ begin {array} {ccccccc}
x_ {0,0} & y_ {0,0} & x_ {1,0} & y_ {1,0} & \\ cdots & x_ {N-1,0} & y_ {N-1,0} \ \\\
x_ {0,1} & y_ {0,1} & x_ {1,1} & y_ {1,1} & \\ cdots & x_ {N-1,1} & y_ {N-1,1} \ \\\
\\ vdots & \\ vdots & \\ vdots & \\ vdots & \\ ddots & \\ vdots & \\ vdots \\\\
x_ {0, N-1} & y_ {0, N-1} & x_ {1, N-1} & y_ {1, N-1} & \\ cdots & x_ {N-1, N-1} & y_ {N-1, N-1}
\\ end {array}


x_ {i, j} and \\ y_ {i, j} represent the x-coordinate and y-coordinate of the jump destination written on the tile at the coordinate (i, j), respectively. The coordinates are 0-origin, and the values ​​of all coordinates are integers greater than or equal to 0 and less than N.

The input ends with a line consisting of only 0s.

Output

For each test case, the number of different loops is output in one line.

Example

Input

1
0 0
2
1 1 0 1
1 0 0 0
2
1 1 0 1
1 1 1 0
3
0 1 2 2 2 1
0 2 1 2 2 1
0 0 0 1 1 1
4
3 2 2 0 3 2 2 1
1 1 0 3 1 1 3 1
0 3 2 3 3 0 2 3
1 1 1 1 3 2 1 3
0


Output

1
2
1
2
3
import java.util.Scanner;

class P{
	int x;
	int y;

	P(int _x, int _y){
		this.x = _x;
		this.y = _y;
	}
}

public class Main {

	static int[][] checked;
	static int n;

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		while(true){
			n = stdIn.nextInt();
			if(n==0){
				return;
			}
			P[][] pos = new P[n][n];
			checked = new int[n][n];
			int loopCnt = 0;
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					int x = stdIn.nextInt();
					int y = stdIn.nextInt();
					pos[i][j] = new P(x,y);
				}
			}

//			for(int i=0;i<n;i++){
//				for(int j=0;j<n;j++){
//					System.out.print(pos[i][j].x+" "+pos[i][j].y);
//				}
//				System.out.println();
//			}

			// Process
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					TwotoOne();
//					System.err.println("テゥツ??"+i+" "+j);
					if(checked[i][j]==0){
						// テ・ツコツァテヲツィツ?i,j)テ」ツ?凝」ツつ嘉」ツδォテ」ツδシテ」ツδ療」ツ?ォテ」ツ?ェテ」ツつ凝ヲツ篠「テァツエツ「テ」ツつケテ」ツつソテ」ツδシテ」ツδ?
//						System.err.println("テ」ツδォテ」ツδシテ」ツδ療ヲツ篠「テァツエツ「テ」ツつケテ」ツつソテ」ツδシテ」ツδ?"+i+" "+j);
						int x = j;
						int y = i;
						boolean inc = true; // テ」ツつ、テ」ツδウテ」ツつッテ」ツδェテ」ツδ。テ」ツδウテ」ツδ暗」ツ?療」ツ?ヲテ」ツつ暗」ツ??」ツ??
						while(true){
							if(checked[y][x]==0){
								// テ」ツ?セテ」ツ??・ツ按ーテゥツ?氾」ツ?療」ツ?ヲテ」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァテァツカツ堙」ツ?妥」ツつ?
								// ティツカツウティツキツ。テ」ツ?、テ」ツ?妥」ツつ?
								checked[y][x] = 2;
								// テヲツャツ。テ」ツ?ョテ・ツコツァテヲツィツ凖」ツつ津ヲツ崢エテヲツ鳴ーテ」ツ?凖」ツつ?
//								System.err.print(x+" "+y+"=>");
								int zx = x;
								int zy = y;
								x = pos[zy][zx].x;
								y = pos[zy][zx].y;
//								System.err.println(""+x+" "+y);

							}
							else if(checked[y][x]==1){
								// テゥツ?偲・ツ篠サテ」ツ?ョテ」ツδォテ」ツδシテ」ツδ療」ツ?ォテァツェツ?・ツ?・テ」ツ?療」ツ??
//								System.err.println("テゥツ?偲・ツ篠サテ」ツ?ョテ」ツδォテ」ツδシテ」ツδ療ァツェツ?・ツ?・テ」ツ?ァテ」ツ?療」ツ??/ テ」ツ?、テ」ツ?・テ」ツ?妥」ツ?セテ」ツ??);
								checked[y][x] = 2;
								inc = false;	// テゥツ?偲・ツ篠サテ」ツδォテ」ツδシテ」ツδ療」ツδ陛」ツδゥテ」ツつー
//								break;
							}
							else if(checked[y][x]==2){
								// テヲツ篠「テァツエツ「テ・ツ慊ーテァツつケテ」ツ?セテ」ツ?ァテヲツ按サテ」ツつ?
//								System.err.println("テァツオツづ、ツコツ?);
								if(inc) {
									// テヲツ篠津、ツサツ姪」ツδォテ」ツδシテ」ツδ療」ツ?ョテァツ卍コティツヲツ?
//									System.err.println("テヲツ篠津、ツサツ姪」ツδォテ」ツδシテ」ツδ療」ツ?ョテァツ卍コティツヲツ?);
									loopCnt++;
								}
								break;  // while(true)テ」ツ?ョbreak
							}
						}
					}
				}
			}
			System.out.println(loopCnt);
		}
	}

	private static void TwotoOne() {
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(checked[i][j]==2){
					checked[i][j] = 1;
				}
			}
		}

	}
}