from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
graph = {i: deque() for i in range(1, n+1)}

for i in range(n):
    u, k, *v = map(int, input().split())
    v.sort()
    for vi in v:
        graph[u].append(vi)

seen = [0]*(n+1)
queue = deque()
dist = [-1]*(n+1)
def bfs(v):
    dist[v] = 0
    seen[v] = 1
    queue.append(v)
    while queue:
        q = queue[0]
        if not graph[q]:
            queue.popleft()
            continue
        g = graph[q].popleft()
        if seen[g]:
            continue
        seen[g] = 1
        dist[g] = dist[q] + 1
        queue.append(g)

bfs(1)
for id, d in enumerate(dist):
    if id == 0:
        continue
    print(id, d) from collections import deque import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n = int(input()) n int(input()) int input() input i: deque() for i in range(1, n+1) for i in range(1, n+1) i range(1, n+1) range 1 n+1 n 1 for i in range(1, n+1) i deque() deque graph = {i: deque() for i in range(1, n+1)} graph {i: deque() for i in range(1, n+1)} for i in range(n):
    u, k, *v = map(int, input().split())
    v.sort()
    for vi in v:
        graph[u].append(vi) i range(n) range n u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v v.sort() v.sort v sort for vi in v:
        graph[u].append(vi) vi v graph[u].append(vi) [u].append [u] graph u append vi seen = [0]*(n+1) seen [0]*(n+1) [0] 0 n+1 n 1 queue = deque() queue deque() deque dist = [-1]*(n+1) dist [-1]*(n+1) [-1] -1 n+1 n 1 def bfs(v):
    dist[v] = 0
    seen[v] = 1
    queue.append(v)
    while queue:
        q = queue[0]
        if not graph[q]:
            queue.popleft()
            continue
        g = graph[q].popleft()
        if seen[g]:
            continue
        seen[g] = 1
        dist[g] = dist[q] + 1
        queue.append(g) bfs dist[v] = 0 dist[v] dist v 0 seen[v] = 1 seen[v] seen v 1 queue.append(v) queue.append queue append v while queue:
        q = queue[0]
        if not graph[q]:
            queue.popleft()
            continue
        g = graph[q].popleft()
        if seen[g]:
            continue
        seen[g] = 1
        dist[g] = dist[q] + 1
        queue.append(g) queue q = queue[0] q queue[0] queue 0 if not graph[q]:
            queue.popleft()
            continue
         not graph[q] graph[q] graph q queue.popleft() queue.popleft queue popleft continue g = graph[q].popleft() g graph[q].popleft() [q].popleft [q] graph q popleft if seen[g]:
            continue
         seen[g] seen g continue seen[g] = 1 seen[g] seen g 1 dist[g] = dist[q] + 1 dist[g] dist g dist[q] + 1 dist[q] dist q 1 queue.append(g) queue.append queue append g v v bfs(1) bfs 1 for id, d in enumerate(dist):
    if id == 0:
        continue
    print(id, d) id d enumerate(dist) enumerate dist if id == 0:
        continue
     id == 0 id 0 continue print(id, d) print id d def bfs(v):
    dist[v] = 0
    seen[v] = 1
    queue.append(v)
    while queue:
        q = queue[0]
        if not graph[q]:
            queue.popleft()
            continue
        g = graph[q].popleft()
        if seen[g]:
            continue
        seen[g] = 1
        dist[g] = dist[q] + 1
        queue.append(g) def bfs(v):
    dist[v] = 0
    seen[v] = 1
    queue.append(v)
    while queue:
        q = queue[0]
        if not graph[q]:
            queue.popleft()
            continue
        g = graph[q].popleft()
        if seen[g]:
            continue
        seen[g] = 1
        dist[g] = dist[q] + 1
        queue.append(g) bfs n = int(input()) int(input()) n queue = deque() deque() queue graph = {i: deque() for i in range(1, n+1)} {i: deque() for i in range(1, n+1)} graph input = sys.stdin.readline sys.stdin.readline input u, k, *v = map(int, input().split()) map(int, input().split()) u k, *v = map(int, input().split()) map(int, input().split()) k seen = [0]*(n+1) [0]*(n+1) seen dist = [-1]*(n+1) [-1]*(n+1) dist
from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
graph = {i: deque() for i in range(1, n+1)}

for i in range(n):
    u, k, *v = map(int, input().split())
    v.sort()
    for vi in v:
        graph[u].append(vi)

seen = [0]*(n+1)
queue = deque()
dist = [-1]*(n+1)
def bfs(v):
    dist[v] = 0
    seen[v] = 1
    queue.append(v)
    while queue:
        q = queue[0]
        if not graph[q]:
            queue.popleft()
            continue
        g = graph[q].popleft()
        if seen[g]:
            continue
        seen[g] = 1
        dist[g] = dist[q] + 1
        queue.append(g)

bfs(1)
for id, d in enumerate(dist):
    if id == 0:
        continue
    print(id, d)
