n = int(input())
a = [[] for i in range(n)]

for i in range(n):
    v = list(map(int, input().split()))
    u = v[0] - 1
    for j in v[2:]:
        a[u].append(j-1)

d = [-1] * n
isDiscovered = [False] * n

def bfs(x):
    for i in a[x]:
        if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1

d_ = 0
isDiscovered [0] = True
d[0] = 0
bfs(0)

flag = True
while flag:
    flag = False
    for i in range(n):
        if d[i] is d_ :
            flag = True
            bfs(i)
    d_ += 1

for i in range(n):
    print(i+1, d[i]) n = int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] a = [[] for i in range(n)] a [[] for i in range(n)] for i in range(n):
    v = list(map(int, input().split()))
    u = v[0] - 1
    for j in v[2:]:
        a[u].append(j-1) i range(n) range n v = list(map(int, input().split())) v list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = v[0] - 1 u v[0] - 1 v[0] v 0 1 for j in v[2:]:
        a[u].append(j-1) j v[2:] v 2: 2 a[u].append(j-1) [u].append [u] a u append j-1 j 1 d = [-1] * n d [-1] * n [-1] -1 n isDiscovered = [False] * n isDiscovered [False] * n [False] False n def bfs(x):
    for i in a[x]:
        if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1 bfs for i in a[x]:
        if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1 i a[x] a x if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1 not isDiscovered[i] isDiscovered[i] isDiscovered i isDiscovered [i] = True isDiscovered [i] isDiscovered i True d[i] = d[x] + 1 d[i] d i d[x] + 1 d[x] d x 1 x x d_ = 0 d_ 0 isDiscovered [0] = True isDiscovered [0] isDiscovered 0 True d[0] = 0 d[0] d 0 0 bfs(0) bfs 0 flag = True flag True while flag:
    flag = False
    for i in range(n):
        if d[i] is d_ :
            flag = True
            bfs(i)
    d_ += 1 flag flag = False flag False for i in range(n):
        if d[i] is d_ :
            flag = True
            bfs(i)
     i range(n) range n if d[i] is d_ :
            flag = True
            bfs(i)
     d[i] is d_ d[i] d i d_ flag = True flag True bfs(i) bfs i d_ += 1 d_ 1 for i in range(n):
    print(i+1, d[i]) i range(n) range n print(i+1, d[i]) print i+1 i 1 d[i] d i flag = True True flag n = int(input()) int(input()) n u = v[0] - 1 v[0] - 1 u a = [[] for i in range(n)] [[] for i in range(n)] a flag = True True flag d_ += 1 1 d_ d_ = 0 0 d_ isDiscovered = [False] * n [False] * n isDiscovered flag = False False flag d = [-1] * n [-1] * n d def bfs(x):
    for i in a[x]:
        if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1 def bfs(x):
    for i in a[x]:
        if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1 bfs v = list(map(int, input().split())) list(map(int, input().split())) v
n = int(input())
a = [[] for i in range(n)]

for i in range(n):
    v = list(map(int, input().split()))
    u = v[0] - 1
    for j in v[2:]:
        a[u].append(j-1)

d = [-1] * n
isDiscovered = [False] * n

def bfs(x):
    for i in a[x]:
        if not isDiscovered[i]:
            isDiscovered [i] = True
            d[i] = d[x] + 1

d_ = 0
isDiscovered [0] = True
d[0] = 0
bfs(0)

flag = True
while flag:
    flag = False
    for i in range(n):
        if d[i] is d_ :
            flag = True
            bfs(i)
    d_ += 1

for i in range(n):
    print(i+1, d[i])