import queue


def bfs(g, s=1):
    global q

    dist = [-1] * len(g)
    dist[1] = 0
    q = queue.Queue()
    q.put(s)

    while not q.empty():
        v = q.get()
        if not G[v][1]:
            continue
        for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

    return dist


n = int(input())
G = [None]
for i in range(n):
    G.append(list(map(int, input().split())))

d = bfs(G)
for i in range(1, n + 1):
    print('%d %d' % (i, d[i])) import queue queue def bfs(g, s=1):
    global q

    dist = [-1] * len(g)
    dist[1] = 0
    q = queue.Queue()
    q.put(s)

    while not q.empty():
        v = q.get()
        if not G[v][1]:
            continue
        for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

    return dist bfs global q dist = [-1] * len(g) dist [-1] * len(g) [-1] -1 len(g) len g dist[1] = 0 dist[1] dist 1 0 q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(s) q.put q put s while not q.empty():
        v = q.get()
        if not G[v][1]:
            continue
        for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

     not q.empty() q.empty() q.empty q empty v = q.get() v q.get() q.get q get if not G[v][1]:
            continue
         not G[v][1] G[v][1] [v] G v 1 continue for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

     vi G[v][2:] [v] G v 2: 2 if dist[vi] >= 0:
                continue
             dist[vi] >= 0 dist[vi] dist vi 0 continue q.put(vi) q.put q put vi dist[vi] = dist[v] + 1 dist[vi] dist vi dist[v] + 1 dist[v] dist v 1 return dist dist g g s=1 s 1 n = int(input()) n int(input()) int input() input G = [None] G [None] None for i in range(n):
    G.append(list(map(int, input().split()))) i range(n) range n G.append(list(map(int, input().split()))) G.append G append list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split d = bfs(G) d bfs(G) bfs G for i in range(1, n + 1):
    print('%d %d' % (i, d[i])) i range(1, n + 1) range 1 n + 1 n 1 print('%d %d' % (i, d[i])) print '%d %d' % (i, d[i]) '%d %d' (i, d[i]) i d[i] d i G = [None] [None] G def bfs(g, s=1):
    global q

    dist = [-1] * len(g)
    dist[1] = 0
    q = queue.Queue()
    q.put(s)

    while not q.empty():
        v = q.get()
        if not G[v][1]:
            continue
        for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

    return dist def bfs(g, s=1):
    global q

    dist = [-1] * len(g)
    dist[1] = 0
    q = queue.Queue()
    q.put(s)

    while not q.empty():
        v = q.get()
        if not G[v][1]:
            continue
        for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

    return dist bfs n = int(input()) int(input()) n d = bfs(G) bfs(G) d
import queue


def bfs(g, s=1):
    global q

    dist = [-1] * len(g)
    dist[1] = 0
    q = queue.Queue()
    q.put(s)

    while not q.empty():
        v = q.get()
        if not G[v][1]:
            continue
        for vi in G[v][2:]:
            if dist[vi] >= 0:
                continue
            q.put(vi)
            dist[vi] = dist[v] + 1

    return dist


n = int(input())
G = [None]
for i in range(n):
    G.append(list(map(int, input().split())))

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