from sys import stdin
from collections import deque
n = int(stdin.readline())
d = [-1] * (n + 1)
def bfs(G):
    d[1] = 0
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x)
G = [0]
for i in range(n):
    G.append(list(map(int, stdin.readline().split()[2:])))
bfs(G) from sys import stdin from collections import deque n = int(stdin.readline()) n int(stdin.readline()) int stdin.readline() stdin.readline stdin readline d = [-1] * (n + 1) d [-1] * (n + 1) [-1] -1 n + 1 n 1 def bfs(G):
    d[1] = 0
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x) bfs d[1] = 0 d[1] d 1 0 dq = deque([1]) dq deque([1]) deque [1] 1 while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
     len(dq) > 0 len(dq) len dq 0 v = dq.popleft() v dq.popleft() dq.popleft dq popleft for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
     c G[v] G v if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
     d[c] < 0 d[c] d c 0 d[c] = d[v] + 1 d[c] d c d[v] + 1 d[v] d v 1 dq.append(c) dq.append dq append c for i, x in enumerate(d[1:], start=1):
        print(i, x) i x enumerate(d[1:], start=1) enumerate d[1:] d 1: 1 start=1 start 1 print(i, x) print i x G G G = [0] G [0] 0 for i in range(n):
    G.append(list(map(int, stdin.readline().split()[2:]))) i range(n) range n G.append(list(map(int, stdin.readline().split()[2:]))) G.append G append list(map(int, stdin.readline().split()[2:])) list map(int, stdin.readline().split()[2:]) map int stdin.readline().split()[2:] stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split 2: 2 bfs(G) bfs G d = [-1] * (n + 1) [-1] * (n + 1) d def bfs(G):
    d[1] = 0
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x) def bfs(G):
    d[1] = 0
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x) bfs G = [0] [0] G n = int(stdin.readline()) int(stdin.readline()) n
from sys import stdin
from collections import deque
n = int(stdin.readline())
d = [-1] * (n + 1)
def bfs(G):
    d[1] = 0
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x)
G = [0]
for i in range(n):
    G.append(list(map(int, stdin.readline().split()[2:])))
bfs(G)