from sys import stdin
from collections import deque

n = int(stdin.readline())
g = [None] * n
for _ in range(n):
    u, _, *cs = [int(s) for s in stdin.readline().split()]
    g[u-1] = [c - 1 for c in cs]
ds = [-1] * n
v = [False] * n
v[0] = True
q = deque([(0, 0)])
while len(q):
    u, d = q.popleft()
    ds[u] = d
    for c in g[u]:
        if not v[c]:
            v[c] = True
            q.append((c, d + 1))
for u, d in enumerate(ds):
    print(u + 1, d) from sys import stdin from collections import deque n = int(stdin.readline()) n int(stdin.readline()) int stdin.readline() stdin.readline stdin readline g = [None] * n g [None] * n [None] None n for _ in range(n):
    u, _, *cs = [int(s) for s in stdin.readline().split()]
    g[u-1] = [c - 1 for c in cs] _ range(n) range n int(s) for s in stdin.readline().split() for s in stdin.readline().split() s stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split for s in stdin.readline().split() int(s) int s u, _, *cs = [int(s) for s in stdin.readline().split()] u [int(s) for s in stdin.readline().split()] _ *cs cs c - 1 for c in cs for c in cs c cs for c in cs c - 1 c 1 g[u-1] = [c - 1 for c in cs] g[u-1] g u-1 u 1 [c - 1 for c in cs] ds = [-1] * n ds [-1] * n [-1] -1 n v = [False] * n v [False] * n [False] False n v[0] = True v[0] v 0 True q = deque([(0, 0)]) q deque([(0, 0)]) deque [(0, 0)] (0, 0) 0 0 while len(q):
    u, d = q.popleft()
    ds[u] = d
    for c in g[u]:
        if not v[c]:
            v[c] = True
            q.append((c, d + 1)) len(q) len q u, d = q.popleft() u q.popleft() q.popleft q popleft d ds[u] = d ds[u] ds u d for c in g[u]:
        if not v[c]:
            v[c] = True
            q.append((c, d + 1)) c g[u] g u if not v[c]:
            v[c] = True
            q.append((c, d + 1)) not v[c] v[c] v c v[c] = True v[c] v c True q.append((c, d + 1)) q.append q append (c, d + 1) c d + 1 d 1 for u, d in enumerate(ds):
    print(u + 1, d) u d enumerate(ds) enumerate ds print(u + 1, d) print u + 1 u 1 d n = int(stdin.readline()) int(stdin.readline()) n ds = [-1] * n [-1] * n ds u, d = q.popleft() q.popleft() u q = deque([(0, 0)]) deque([(0, 0)]) q g = [None] * n [None] * n g v = [False] * n [False] * n v _, *cs = [int(s) for s in stdin.readline().split()] [int(s) for s in stdin.readline().split()] _ d = q.popleft() q.popleft() d u, _, *cs = [int(s) for s in stdin.readline().split()] [int(s) for s in stdin.readline().split()] u
from sys import stdin
from collections import deque

n = int(stdin.readline())
g = [None] * n
for _ in range(n):
    u, _, *cs = [int(s) for s in stdin.readline().split()]
    g[u-1] = [c - 1 for c in cs]
ds = [-1] * n
v = [False] * n
v[0] = True
q = deque([(0, 0)])
while len(q):
    u, d = q.popleft()
    ds[u] = d
    for c in g[u]:
        if not v[c]:
            v[c] = True
            q.append((c, d + 1))
for u, d in enumerate(ds):
    print(u + 1, d)