from collections import deque

n = int(input())
g = [None] * (n+1)
ans = [0] * (n+1)

for _ in range(n):
    u,k,*vk = map(int,input().split())
    g[u] = vk

def bfs(now):
    que = deque()
    que.append(now)
    while que.__len__() != 0:
        now = que.popleft()
        for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne)

bfs(1)
ans = [i if i != 0 else -1 for i in ans]
ans[1] = 0

print("\n".join("{} {}".format(*args) for args in zip(range(1, n+1), ans[1:]))) from collections import deque n = int(input()) n int(input()) int input() input g = [None] * (n+1) g [None] * (n+1) [None] None n+1 n 1 ans = [0] * (n+1) ans [0] * (n+1) [0] 0 n+1 n 1 for _ in range(n):
    u,k,*vk = map(int,input().split())
    g[u] = vk _ range(n) range n u,k,*vk = map(int,input().split()) u map(int,input().split()) map int input().split() ().split () input split k *vk vk g[u] = vk g[u] g u vk def bfs(now):
    que = deque()
    que.append(now)
    while que.__len__() != 0:
        now = que.popleft()
        for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne) bfs que = deque() que deque() deque que.append(now) que.append que append now while que.__len__() != 0:
        now = que.popleft()
        for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne) que.__len__() != 0 que.__len__() que.__len__ que __len__ 0 now = que.popleft() now que.popleft() que.popleft que popleft for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne) ne g[now] g now if ans[ne] != 0:
                continue
             ans[ne] != 0 ans[ne] ans ne 0 continue ans[ne] = ans[now]+1 ans[ne] ans ne ans[now]+1 ans[now] ans now 1 que.append(ne) que.append que append ne now now bfs(1) bfs 1 i if i != 0 else -1 for i in ans for i in ans i ans for i in ans i if i != 0 else -1 i != 0 i 0 i -1 ans = [i if i != 0 else -1 for i in ans] ans [i if i != 0 else -1 for i in ans] ans[1] = 0 ans[1] ans 1 0 print("\n".join("{} {}".format(*args) for args in zip(range(1, n+1), ans[1:]))) print "\n".join("{} {}".format(*args) for args in zip(range(1, n+1), ans[1:])) "\n".join "\n" join "{} {}".format(*args) "{} {}".format "{} {}" format *args args k,*vk = map(int,input().split()) map(int,input().split()) k def bfs(now):
    que = deque()
    que.append(now)
    while que.__len__() != 0:
        now = que.popleft()
        for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne) def bfs(now):
    que = deque()
    que.append(now)
    while que.__len__() != 0:
        now = que.popleft()
        for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne) bfs ans = [0] * (n+1) [0] * (n+1) ans g = [None] * (n+1) [None] * (n+1) g u,k,*vk = map(int,input().split()) map(int,input().split()) u ans = [i if i != 0 else -1 for i in ans] [i if i != 0 else -1 for i in ans] ans n = int(input()) int(input()) n
from collections import deque

n = int(input())
g = [None] * (n+1)
ans = [0] * (n+1)

for _ in range(n):
    u,k,*vk = map(int,input().split())
    g[u] = vk

def bfs(now):
    que = deque()
    que.append(now)
    while que.__len__() != 0:
        now = que.popleft()
        for ne in g[now]:
            if ans[ne] != 0:
                continue
            ans[ne] = ans[now]+1
            que.append(ne)

bfs(1)
ans = [i if i != 0 else -1 for i in ans]
ans[1] = 0

print("\n".join("{} {}".format(*args) for args in zip(range(1, n+1), ans[1:])))
