from collections import deque
n = int(input())
M = [[0]*n for _ in range(n)]
d = [-1]*n
st = [0]*n
for _ in range(n):
    i = [int(i) for i in input().split()]
    u = i[0]
    k = i[1]
    V = i[2:]
    for v in V: M[u-1][v-1] = 1

def bfs(s):
    Q = deque()
    Q.append(s)
    st[s] = 1
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
    return

bfs(0)
for i in range(n): print(i+1,d[i]) from collections import deque n = int(input()) n int(input()) int input() input [0]*n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0]*n [0] 0 n M = [[0]*n for _ in range(n)] M [[0]*n for _ in range(n)] d = [-1]*n d [-1]*n [-1] -1 n st = [0]*n st [0]*n [0] 0 n for _ in range(n):
    i = [int(i) for i in input().split()]
    u = i[0]
    k = i[1]
    V = i[2:]
    for v in V: M[u-1][v-1] = 1 _ range(n) range n int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i i = [int(i) for i in input().split()] i [int(i) for i in input().split()] u = i[0] u i[0] i 0 k = i[1] k i[1] i 1 V = i[2:] V i[2:] i 2: 2 for v in V: M[u-1][v-1] v V def bfs(s):
    Q = deque()
    Q.append(s)
    st[s] = 1
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
    return bfs Q = deque() Q deque() deque Q.append(s) Q.append Q append s st[s] = 1 st[s] st s 1 d[s] = 0 d[s] d s 0 while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
     Q u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
         v range(n) range n if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
         M[u][v] and st[v] == 0 M[u][v] [u] M u v st[v] == 0 st[v] st v 0 st[v] = 1 st[v] st v 1 Q.append(v) Q.append Q append v d[v] = d[u]+1 d[v] d v d[u]+1 d[u] d u 1 st[u] = 2 st[u] st u 2 return s s bfs(0) bfs 0 for i in range(n): print(i+1,d[i]) i range(n) range n i = [int(i) for i in input().split()] [int(i) for i in input().split()] i st = [0]*n [0]*n st V = i[2:] i[2:] V n = int(input()) int(input()) n def bfs(s):
    Q = deque()
    Q.append(s)
    st[s] = 1
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
    return def bfs(s):
    Q = deque()
    Q.append(s)
    st[s] = 1
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
    return bfs d = [-1]*n [-1]*n d k = i[1] i[1] k M = [[0]*n for _ in range(n)] [[0]*n for _ in range(n)] M u = i[0] i[0] u
from collections import deque
n = int(input())
M = [[0]*n for _ in range(n)]
d = [-1]*n
st = [0]*n
for _ in range(n):
    i = [int(i) for i in input().split()]
    u = i[0]
    k = i[1]
    V = i[2:]
    for v in V: M[u-1][v-1] = 1

def bfs(s):
    Q = deque()
    Q.append(s)
    st[s] = 1
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
    return

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