from collections import deque
n = int(input())
G = [[0]*n for _ in range(n)]
for _ in range(n):
    u, k, *V = map(int, input().split())
    if k == 0:
        continue
    else:
        for v in V:
            G[u-1][v-1] = 1



def bfs(u=0):
    Q = deque([])
    color = ['White']*n
    d = [-1]*n

    d[0] = 0
    Q.append(u)
    while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

    return d

D = bfs()
for i in range(n):
    print('{} {}'.format(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 G = [[0]*n for _ in range(n)] G [[0]*n for _ in range(n)] for _ in range(n):
    u, k, *V = map(int, input().split())
    if k == 0:
        continue
    else:
        for v in V:
            G[u-1][v-1] = 1 _ 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 if k == 0:
        continue
    else:
        for v in V:
            G[u-1][v-1] = 1 k == 0 k 0 continue for v in V:
            G[u-1][v-1] = 1 v V G[u-1][v-1] = 1 G[u-1][v-1] [u-1] G u-1 u 1 v-1 v 1 1 def bfs(u=0):
    Q = deque([])
    color = ['White']*n
    d = [-1]*n

    d[0] = 0
    Q.append(u)
    while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

    return d bfs Q = deque([]) Q deque([]) deque [] color = ['White']*n color ['White']*n ['White'] 'White' n d = [-1]*n d [-1]*n [-1] -1 n d[0] = 0 d[0] d 0 0 Q.append(u) Q.append Q append u while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

     Q u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
         v range(n) range n if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
         G[u][v] == 1 and color[v] == 'White' G[u][v] == 1 G[u][v] [u] G u v 1 color[v] == 'White' color[v] color v 'White' if d[v] != -1:
                    continue
                 d[v] != -1 d[v] d v -1 continue color[v] = 'Gray' color[v] color v 'Gray' d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 Q.append(v) Q.append Q append v color[u] = 'Black' color[u] color u 'Black' return d d u=0 u 0 D = bfs() D bfs() bfs for i in range(n):
    print('{} {}'.format(i+1, D[i])) i range(n) range n print('{} {}'.format(i+1, D[i])) print '{} {}'.format(i+1, D[i]) '{} {}'.format '{} {}' format i+1 i 1 D[i] D i k, *V = map(int, input().split()) map(int, input().split()) k def bfs(u=0):
    Q = deque([])
    color = ['White']*n
    d = [-1]*n

    d[0] = 0
    Q.append(u)
    while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

    return d def bfs(u=0):
    Q = deque([])
    color = ['White']*n
    d = [-1]*n

    d[0] = 0
    Q.append(u)
    while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

    return d bfs n = int(input()) int(input()) n D = bfs() bfs() D G = [[0]*n for _ in range(n)] [[0]*n for _ in range(n)] G u, k, *V = map(int, input().split()) map(int, input().split()) u
from collections import deque
n = int(input())
G = [[0]*n for _ in range(n)]
for _ in range(n):
    u, k, *V = map(int, input().split())
    if k == 0:
        continue
    else:
        for v in V:
            G[u-1][v-1] = 1



def bfs(u=0):
    Q = deque([])
    color = ['White']*n
    d = [-1]*n

    d[0] = 0
    Q.append(u)
    while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

    return d

D = bfs()
for i in range(n):
    print('{} {}'.format(i+1, D[i]))

