import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

def bfs(start):
    d = [-1]*n
    d[start] = 0
    q = collections.deque([start])
    while q:
        p = q.popleft()
        next_v = edge[p]
        for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
    return d

if __name__ == "__main__":
    n = int(input())
    edge = []
    for i in range(n):
        u,k,*v_li = map(int,input().split())
        u-=1
        for j in range(len(v_li)):
            v_li[j] -= 1
        edge.append(v_li)
    ans = bfs(0)
    for i in range(n):
        print(i+1,ans[i])
 import sys sys import itertools itertools sys.setrecursionlimit(1000000000) sys.setrecursionlimit sys setrecursionlimit 1000000000 from heapq import heapify,heappop,heappush,heappushpop import math math import collections collections import copy copy def bfs(start):
    d = [-1]*n
    d[start] = 0
    q = collections.deque([start])
    while q:
        p = q.popleft()
        next_v = edge[p]
        for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
    return d bfs d = [-1]*n d [-1]*n [-1] -1 n d[start] = 0 d[start] d start 0 q = collections.deque([start]) q collections.deque([start]) collections.deque collections deque [start] start while q:
        p = q.popleft()
        next_v = edge[p]
        for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
     q p = q.popleft() p q.popleft() q.popleft q popleft next_v = edge[p] next_v edge[p] edge p for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
     v next_v if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
     d[v] == -1 d[v] d v -1 q.append(v) q.append q append v d[v] = d[p] + 1 d[v] d v d[p] + 1 d[p] d p 1 return d d start start if __name__ == "__main__":
    n = int(input())
    edge = []
    for i in range(n):
        u,k,*v_li = map(int,input().split())
        u-=1
        for j in range(len(v_li)):
            v_li[j] -= 1
        edge.append(v_li)
    ans = bfs(0)
    for i in range(n):
        print(i+1,ans[i]) __name__ == "__main__" __name__ "__main__" n = int(input()) n int(input()) int input() input edge = [] edge [] for i in range(n):
        u,k,*v_li = map(int,input().split())
        u-=1
        for j in range(len(v_li)):
            v_li[j] -= 1
        edge.append(v_li)
     i range(n) range n u,k,*v_li = map(int,input().split()) u map(int,input().split()) map int input().split() ().split () input split k *v_li v_li u-=1 u 1 for j in range(len(v_li)):
            v_li[j] -= 1
         j range(len(v_li)) range len(v_li) len v_li v_li[j] -= 1 v_li[j] v_li j 1 edge.append(v_li) edge.append edge append v_li ans = bfs(0) ans bfs(0) bfs 0 for i in range(n):
        print(i+1,ans[i]) i range(n) range n print(i+1,ans[i]) print i+1 i 1 ans[i] ans i def bfs(start):
    d = [-1]*n
    d[start] = 0
    q = collections.deque([start])
    while q:
        p = q.popleft()
        next_v = edge[p]
        for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
    return d def bfs(start):
    d = [-1]*n
    d[start] = 0
    q = collections.deque([start])
    while q:
        p = q.popleft()
        next_v = edge[p]
        for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
    return d bfs edge = [] [] edge u-=1 1 u ans = bfs(0) bfs(0) ans n = int(input()) int(input()) n u,k,*v_li = map(int,input().split()) map(int,input().split()) u k,*v_li = map(int,input().split()) map(int,input().split()) k
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

def bfs(start):
    d = [-1]*n
    d[start] = 0
    q = collections.deque([start])
    while q:
        p = q.popleft()
        next_v = edge[p]
        for v in next_v:
            if d[v] == -1:
                q.append(v)
                d[v] = d[p] + 1
    return d

if __name__ == "__main__":
    n = int(input())
    edge = []
    for i in range(n):
        u,k,*v_li = map(int,input().split())
        u-=1
        for j in range(len(v_li)):
            v_li[j] -= 1
        edge.append(v_li)
    ans = bfs(0)
    for i in range(n):
        print(i+1,ans[i])

