n = int(input())

m = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    com = list(map(int, input().split()))
    for j in range(com[1]):
        m[i][com[j+2]-1] = 1

color = ["white" for i in range(n)]

from collections import deque
queue = deque()

d = [-1 for i in range(n)]

def bfs():
    color[0] = "gray"
    d[0] = 0
    queue.append(0)
    while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black"

bfs()
for i in range(n):
    print(i+1, d[i]) n = int(input()) n int(input()) int input() input [0 for i in range(n)] for j in range(n) for j in range(n) j range(n) range n for j in range(n) [0 for i in range(n)] m = [[0 for i in range(n)] for j in range(n)] m [[0 for i in range(n)] for j in range(n)] for i in range(n):
    com = list(map(int, input().split()))
    for j in range(com[1]):
        m[i][com[j+2]-1] = 1 i range(n) range n com = list(map(int, input().split())) com list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for j in range(com[1]):
        m[i][com[j+2]-1] = 1 j range(com[1]) range com[1] com 1 m[i][com[j+2]-1] = 1 m[i][com[j+2]-1] [i] m i com[j+2]-1 com[j+2] com j+2 j 2 1 1 "white" for i in range(n) for i in range(n) i range(n) range n for i in range(n) "white" color = ["white" for i in range(n)] color ["white" for i in range(n)] from collections import deque queue = deque() queue deque() deque -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 d = [-1 for i in range(n)] d [-1 for i in range(n)] def bfs():
    color[0] = "gray"
    d[0] = 0
    queue.append(0)
    while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black" bfs color[0] = "gray" color[0] color 0 "gray" d[0] = 0 d[0] d 0 0 queue.append(0) queue.append queue append 0 while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black" len(queue) != 0 len(queue) len queue 0 u = queue.popleft() u queue.popleft() queue.popleft queue popleft for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
         i range(n) range n if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
         m[u][i] and color[i] == "white" m[u][i] [u] m u i color[i] == "white" color[i] color i "white" color[i] = "gray" color[i] color i "gray" d[i] = d[u] + 1 d[i] d i d[u] + 1 d[u] d u 1 queue.append(i) queue.append queue append i color[u] = "black" color[u] color u "black" bfs() bfs for i in range(n):
    print(i+1, d[i]) i range(n) range n print(i+1, d[i]) print i+1 i 1 d[i] d i n = int(input()) int(input()) n d = [-1 for i in range(n)] [-1 for i in range(n)] d queue = deque() deque() queue def bfs():
    color[0] = "gray"
    d[0] = 0
    queue.append(0)
    while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black" def bfs():
    color[0] = "gray"
    d[0] = 0
    queue.append(0)
    while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black" bfs m = [[0 for i in range(n)] for j in range(n)] [[0 for i in range(n)] for j in range(n)] m color = ["white" for i in range(n)] ["white" for i in range(n)] color com = list(map(int, input().split())) list(map(int, input().split())) com
n = int(input())

m = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    com = list(map(int, input().split()))
    for j in range(com[1]):
        m[i][com[j+2]-1] = 1

color = ["white" for i in range(n)]

from collections import deque
queue = deque()

d = [-1 for i in range(n)]

def bfs():
    color[0] = "gray"
    d[0] = 0
    queue.append(0)
    while len(queue) != 0:
        u = queue.popleft()
        for i in range(n):
            if m[u][i] and color[i] == "white":
                color[i] = "gray"
                d[i] = d[u] + 1
                queue.append(i)
        color[u] = "black"

bfs()
for i in range(n):
    print(i+1, d[i])
