# -*- coding: utf-8 -*-
"""
Created on Sun Jul  8 10:56:33 2018

@author: maezawa
"""
n = int(input())
adj = [[] for _ in range(n)]
d = [-1 for _ in range(n)]
d[0] = 0


for j in range(n):
    ain = list(map(int, input().split()))
    u = ain[0]
    k = ain[1]
    for i in range(k):
        adj[u-1].append(ain[i+2]-1)
    adj[u-1].sort()
    #print(*adj[u-1])

stack = [0]

while stack:
    #print(stack)
    current = stack[-1]
    flag = 0
    for k in adj[current]:
        if d[k] < 0:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
        elif d[k] > d[current]+1:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
    if flag == 0:
        stack.pop()

    #print('current = {}, t = {}'.format(current, t))
    #print('stack', *stack)

for i in range(n):
    print('{} {}'.format(i+1, d[i]))
 """
Created on Sun Jul  8 10:56:33 2018

@author: maezawa
""" n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] adj = [[] for _ in range(n)] adj [[] for _ in range(n)] -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 d = [-1 for _ in range(n)] d [-1 for _ in range(n)] d[0] = 0 d[0] d 0 0 for j in range(n):
    ain = list(map(int, input().split()))
    u = ain[0]
    k = ain[1]
    for i in range(k):
        adj[u-1].append(ain[i+2]-1)
    adj[u-1].sort()
    #print(*adj[u-1]) j range(n) range n ain = list(map(int, input().split())) ain list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = ain[0] u ain[0] ain 0 k = ain[1] k ain[1] ain 1 for i in range(k):
        adj[u-1].append(ain[i+2]-1)
     i range(k) range k adj[u-1].append(ain[i+2]-1) [u-1].append [u-1] adj u-1 u 1 append ain[i+2]-1 ain[i+2] ain i+2 i 2 1 adj[u-1].sort() [u-1].sort [u-1] adj u-1 u 1 sort stack = [0] stack [0] 0 while stack:
    #print(stack)
    current = stack[-1]
    flag = 0
    for k in adj[current]:
        if d[k] < 0:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
        elif d[k] > d[current]+1:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
    if flag == 0:
        stack.pop()

    #print('current = {}, t = {}'.format(current, t))
    #print('stack', *stack) stack current = stack[-1] current stack[-1] stack -1 flag = 0 flag 0 for k in adj[current]:
        if d[k] < 0:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
        elif d[k] > d[current]+1:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
     k adj[current] adj current if d[k] < 0:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
        elif d[k] > d[current]+1:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
     d[k] < 0 d[k] d k 0 d[k] = d[current]+1 d[k] d k d[current]+1 d[current] d current 1 stack.append(k) stack.append stack append k flag = 1 flag 1 elif d[k] > d[current]+1:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
     d[k] > d[current]+1 d[k] d k d[current]+1 d[current] d current 1 d[k] = d[current]+1 d[k] d k d[current]+1 d[current] d current 1 stack.append(k) stack.append stack append k flag = 1 flag 1 if flag == 0:
        stack.pop()

    #print('current = {}, t = {}'.format(current, t))
    #print('stack', *stack) flag == 0 flag 0 stack.pop() stack.pop stack pop 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 current = stack[-1] stack[-1] current flag = 1 1 flag flag = 0 0 flag u = ain[0] ain[0] u ain = list(map(int, input().split())) list(map(int, input().split())) ain k = ain[1] ain[1] k flag = 1 1 flag n = int(input()) int(input()) n adj = [[] for _ in range(n)] [[] for _ in range(n)] adj d = [-1 for _ in range(n)] [-1 for _ in range(n)] d stack = [0] [0] stack
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  8 10:56:33 2018

@author: maezawa
"""
n = int(input())
adj = [[] for _ in range(n)]
d = [-1 for _ in range(n)]
d[0] = 0


for j in range(n):
    ain = list(map(int, input().split()))
    u = ain[0]
    k = ain[1]
    for i in range(k):
        adj[u-1].append(ain[i+2]-1)
    adj[u-1].sort()
    #print(*adj[u-1])

stack = [0]

while stack:
    #print(stack)
    current = stack[-1]
    flag = 0
    for k in adj[current]:
        if d[k] < 0:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
        elif d[k] > d[current]+1:
            d[k] = d[current]+1
            stack.append(k)
            flag = 1
    if flag == 0:
        stack.pop()

    #print('current = {}, t = {}'.format(current, t))
    #print('stack', *stack)

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

