Find out my number neighbors The input consists of i rows with neighbors information. Each ith row contains 4 values, representing the neighbor of i to the North, East, South and West directions, respectively. So each value represents a neighbor at the given direction of the ith row, starting from row 1, and can go up to 65,535 rows. The 0 value indicates no neighbor to that direction.

For instance, if the first row is "0 2 3 10" it means that the i neighbor has three other neighbors: no one to the north, neighbor 2 to the east, neighbor 3 to the south and neighbor 10 to the west.

You need to output the array of neighbors, starting from the value which is most to the northwest. Each neighbor will be displayed only once, at its position relative to others. Let's see some examples:

Input:

0 0 0 0


No neighbors (empty case), output:

1


Input:

0 2 0 0 
0 0 0 1


1 has neighbor 2 to the east. 2 has neighbor 1 to the west

Output:

1 2


Input:

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


1 has neighbor 2 to the east. 2 has neighbor 1 to the west and 3 to the south. 
3 has neighbor 2 to the north

Output:

1 2
  3


Input:

2 0 0 0
0 0 1 0


Output:

2
1


Input:

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


Output:

1 2
3 4


Rules:


Test cases are separated by one empty line. Output of different test cases must also be separated by one empty line.
The output graph is always connected. You are not going to have 1 neighbor to 2 only, along with 3 neighbor to 4 only (isolated from 1-2 component).
All entries are valid. Example of invalid entries: 


Entries containing letters or any symbol different than spaces, line breaks and digits (0-9).
the ith row containing the ith value (because one can't be its own neighbor).
a negative value or value higher than 65,535.
Less than four values in a row. 
More than four values in a row. 
The same neighbor pointing to two different directions (ex: 0 1 1 0).



Standard loopholes apply, and the shortest answer in bytes wins.
Python 2, 152 bytes



l=input()
def f(x,y,n):
 if m[x][y]&lt;n:m[x][y]=n;[f(i%3-1+x,i/3-1+y,h)for h,i in zip(l[n-1],[3,7,5,1])]
e=len(l)
m=eval(`[[0]*e*2]*e*2`)
f(e,e,1)
print m


Try it online!

The input order is NESW
f is a recursive function to populate the houses