from __future__ import print_function
import time
from sys import stdin
from Queue import Queue
start = time.clock()
i = 0
num=int(stdin.readline())
L=[]
for _ in range(num):
    L.append([int(s) for s in stdin.readline().split()[2:]])
 
d=[-1]*num
d[0]=0
q=Queue(100)
q.put(0)
while not q.empty():
    u=q.get()
    for v in L[u]:
        if d[v-1]<0:
            d[v-1]=d[u]+1
            q.put(v-1)
for i,v in enumerate(d):
    print (i+1,v) from __future__ import print_function import time time from sys import stdin from Queue import Queue start = time.clock() start time.clock() time.clock time clock i = 0 i 0 num=int(stdin.readline()) num int(stdin.readline()) int stdin.readline() stdin.readline stdin readline L=[] L [] for _ in range(num):
    L.append([int(s) for s in stdin.readline().split()[2:]])
  _ range(num) range num int(s) for s in stdin.readline().split()[2:] for s in stdin.readline().split()[2:] s stdin.readline().split()[2:] stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split 2: 2 for s in stdin.readline().split()[2:] int(s) int s L.append([int(s) for s in stdin.readline().split()[2:]]) L.append L append [int(s) for s in stdin.readline().split()[2:]] d=[-1]*num d [-1]*num [-1] -1 num d[0]=0 d[0] d 0 0 q=Queue(100) q Queue(100) Queue 100 q.put(0) q.put q put 0 while not q.empty():
    u=q.get()
    for v in L[u]:
        if d[v-1]<0:
            d[v-1]=d[u]+1
            q.put(v-1) not q.empty() q.empty() q.empty q empty u=q.get() u q.get() q.get q get for v in L[u]:
        if d[v-1]<0:
            d[v-1]=d[u]+1
            q.put(v-1) v L[u] L u if d[v-1]<0:
            d[v-1]=d[u]+1
            q.put(v-1) d[v-1]<0 d[v-1] d v-1 v 1 0 d[v-1]=d[u]+1 d[v-1] d v-1 v 1 d[u]+1 d[u] d u 1 q.put(v-1) q.put q put v-1 v 1 for i,v in enumerate(d):
    print (i+1,v) i v enumerate(d) enumerate d print (i+1,v) print i+1 i 1 v i = 0 0 i L=[] [] L num=int(stdin.readline()) int(stdin.readline()) num q=Queue(100) Queue(100) q start = time.clock() time.clock() start d=[-1]*num [-1]*num d u=q.get() q.get() u
from __future__ import print_function
import time
from sys import stdin
from Queue import Queue
start = time.clock()
i = 0
num=int(stdin.readline())
L=[]
for _ in range(num):
    L.append([int(s) for s in stdin.readline().split()[2:]])
 
d=[-1]*num
d[0]=0
q=Queue(100)
q.put(0)
while not q.empty():
    u=q.get()
    for v in L[u]:
        if d[v-1]<0:
            d[v-1]=d[u]+1
            q.put(v-1)
for i,v in enumerate(d):
    print (i+1,v)