from collections import deque


def matrix(n):
    for _ in range(n):
        adj = list(map(int, input().split()))
        i = adj[0]
        v = adj[2:]
        for j in v:
            mat[i - 1][j - 1] = 1


def bfs(v):
    d[v] = 1
    dist[v] = 0
    dq.append(v)
    while dq:
        v = dq.popleft()
        for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n)

n = int(input())
mat = [[0] * n for _ in range(n)]
d, dist = [0] * n, [-1] * n
dq = deque()
matrix(n)
bfs(0)
for i, v in enumerate(dist):
    print(i + 1, v) from collections import deque def matrix(n):
    for _ in range(n):
        adj = list(map(int, input().split()))
        i = adj[0]
        v = adj[2:]
        for j in v:
            mat[i - 1][j - 1] = 1 matrix for _ in range(n):
        adj = list(map(int, input().split()))
        i = adj[0]
        v = adj[2:]
        for j in v:
            mat[i - 1][j - 1] = 1 _ range(n) range n adj = list(map(int, input().split())) adj list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split i = adj[0] i adj[0] adj 0 v = adj[2:] v adj[2:] adj 2: 2 for j in v:
            mat[i - 1][j - 1] = 1 j v mat[i - 1][j - 1] = 1 mat[i - 1][j - 1] [i - 1] mat i - 1 i 1 j - 1 j 1 1 n n def bfs(v):
    d[v] = 1
    dist[v] = 0
    dq.append(v)
    while dq:
        v = dq.popleft()
        for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n) bfs d[v] = 1 d[v] d v 1 dist[v] = 0 dist[v] dist v 0 dq.append(v) dq.append dq append v while dq:
        v = dq.popleft()
        for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n) dq v = dq.popleft() v dq.popleft() dq.popleft dq popleft for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n) n i enumerate(mat[v]) enumerate mat[v] mat v if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n) i == 1 and not d[n] i == 1 i 1 not d[n] d[n] d n d[n] = 1 d[n] d n 1 dist[n] = dist[v] + 1 dist[n] dist n dist[v] + 1 dist[v] dist v 1 dq.append(n) dq.append dq append n v v 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 mat = [[0] * n for _ in range(n)] mat [[0] * n for _ in range(n)] d, dist = [0] * n, [-1] * n d [0] * n [0] 0 n dist [-1] * n [-1] -1 n dq = deque() dq deque() deque matrix(n) matrix n bfs(0) bfs 0 for i, v in enumerate(dist):
    print(i + 1, v) i v enumerate(dist) enumerate dist print(i + 1, v) print i + 1 i 1 v dist = [0] * n, [-1] * n [-1] * n dist n = int(input()) int(input()) n d, dist = [0] * n, [-1] * n [0] * n d dq = deque() deque() dq mat = [[0] * n for _ in range(n)] [[0] * n for _ in range(n)] mat def matrix(n):
    for _ in range(n):
        adj = list(map(int, input().split()))
        i = adj[0]
        v = adj[2:]
        for j in v:
            mat[i - 1][j - 1] = 1 def matrix(n):
    for _ in range(n):
        adj = list(map(int, input().split()))
        i = adj[0]
        v = adj[2:]
        for j in v:
            mat[i - 1][j - 1] = 1 matrix def bfs(v):
    d[v] = 1
    dist[v] = 0
    dq.append(v)
    while dq:
        v = dq.popleft()
        for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n) def bfs(v):
    d[v] = 1
    dist[v] = 0
    dq.append(v)
    while dq:
        v = dq.popleft()
        for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n) bfs
from collections import deque


def matrix(n):
    for _ in range(n):
        adj = list(map(int, input().split()))
        i = adj[0]
        v = adj[2:]
        for j in v:
            mat[i - 1][j - 1] = 1


def bfs(v):
    d[v] = 1
    dist[v] = 0
    dq.append(v)
    while dq:
        v = dq.popleft()
        for n, i in enumerate(mat[v]):
            if i == 1 and not d[n]:
                d[n] = 1
                dist[n] = dist[v] + 1
                dq.append(n)

n = int(input())
mat = [[0] * n for _ in range(n)]
d, dist = [0] * n, [-1] * n
dq = deque()
matrix(n)
bfs(0)
for i, v in enumerate(dist):
    print(i + 1, v)