n = int(input())
Adj = [[0 for i in range(n)] for i in range(n)]
 
for i in range(n):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
 
color = [0] * n
d = [-1] * n
import queue
Q = queue.Queue()

color[0] = 1
d[0] = 0
Q.put(1)
while not Q.empty():
    u = Q.get()
    for v in range(1, n+1):
        if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
    color[u-1] = 2

for i in range(n):
    print("{} {}".format(i + 1, d[i])) n = int(input()) n int(input()) int input() input [0 for i in range(n)] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0 for i in range(n)] Adj = [[0 for i in range(n)] for i in range(n)] Adj [[0 for i in range(n)] for i in range(n)] for i in range(n):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
  i range(n) range n u = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
  u[1] > 0 u[1] u 1 0 for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
  i u[2: 2 + u[1]] u 2: 2 + u[1] 2 2 + u[1] 2 u[1] u 1 Adj[u[0] - 1][i - 1] = 1 Adj[u[0] - 1][i - 1] [u[0] - 1] Adj u[0] - 1 u[0] u 0 1 i - 1 i 1 1 color = [0] * n color [0] * n [0] 0 n d = [-1] * n d [-1] * n [-1] -1 n import queue queue Q = queue.Queue() Q queue.Queue() queue.Queue queue Queue color[0] = 1 color[0] color 0 1 d[0] = 0 d[0] d 0 0 Q.put(1) Q.put Q put 1 while not Q.empty():
    u = Q.get()
    for v in range(1, n+1):
        if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
    color[u-1] = 2 not Q.empty() Q.empty() Q.empty Q empty u = Q.get() u Q.get() Q.get Q get for v in range(1, n+1):
        if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
     v range(1, n+1) range 1 n+1 n 1 if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
     Adj[u-1][v-1] == 1 and color[v-1] == 0 Adj[u-1][v-1] == 1 Adj[u-1][v-1] [u-1] Adj u-1 u 1 v-1 v 1 1 color[v-1] == 0 color[v-1] color v-1 v 1 0 color[v-1] = 1 color[v-1] color v-1 v 1 1 d[v-1] = d[u-1] + 1 d[v-1] d v-1 v 1 d[u-1] + 1 d[u-1] d u-1 u 1 1 Q.put(v) Q.put Q put v color[u-1] = 2 color[u-1] color u-1 u 1 2 for i in range(n):
    print("{} {}".format(i + 1, d[i])) i range(n) range n print("{} {}".format(i + 1, d[i])) print "{} {}".format(i + 1, d[i]) "{} {}".format "{} {}" format i + 1 i 1 d[i] d i color = [0] * n [0] * n color Q = queue.Queue() queue.Queue() Q u = Q.get() Q.get() u Adj = [[0 for i in range(n)] for i in range(n)] [[0 for i in range(n)] for i in range(n)] Adj d = [-1] * n [-1] * n d n = int(input()) int(input()) n u = list(map(int, input().split())) list(map(int, input().split())) u
n = int(input())
Adj = [[0 for i in range(n)] for i in range(n)]
 
for i in range(n):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
 
color = [0] * n
d = [-1] * n
import queue
Q = queue.Queue()

color[0] = 1
d[0] = 0
Q.put(1)
while not Q.empty():
    u = Q.get()
    for v in range(1, n+1):
        if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
    color[u-1] = 2

for i in range(n):
    print("{} {}".format(i + 1, d[i]))