import sys
from collections import deque

inf = 1<<30

def solve():
    n = int(input())
    Adj = [None] * n

    for i in range(n):
        u, k, *line = [int(i) - 1 for i in input().split()]
        Adj[u] = line

    d = bfs(n, Adj)

    for i, di in enumerate(d, start=1):
        if di == inf:
            print(i, -1)
        else:
            print(i, di)

def bfs(n, Adj):
    d = [inf] * n
    d[0] = 0
    nxts = deque([0])

    while nxts:
        u = nxts.popleft()

        for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

    return d

if __name__ == '__main__':
    solve() import sys sys from collections import deque inf = 1<<30 inf 1<<30 1 30 def solve():
    n = int(input())
    Adj = [None] * n

    for i in range(n):
        u, k, *line = [int(i) - 1 for i in input().split()]
        Adj[u] = line

    d = bfs(n, Adj)

    for i, di in enumerate(d, start=1):
        if di == inf:
            print(i, -1)
        else:
            print(i, di) solve n = int(input()) n int(input()) int input() input Adj = [None] * n Adj [None] * n [None] None n for i in range(n):
        u, k, *line = [int(i) - 1 for i in input().split()]
        Adj[u] = line

     i range(n) range n int(i) - 1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) - 1 int(i) int i 1 u, k, *line = [int(i) - 1 for i in input().split()] u [int(i) - 1 for i in input().split()] k *line line Adj[u] = line Adj[u] Adj u line d = bfs(n, Adj) d bfs(n, Adj) bfs n Adj for i, di in enumerate(d, start=1):
        if di == inf:
            print(i, -1)
        else:
            print(i, di) i di enumerate(d, start=1) enumerate d start=1 start 1 if di == inf:
            print(i, -1)
        else:
            print(i, di) di == inf di inf print(i, -1) print i -1 print(i, di) print i di def bfs(n, Adj):
    d = [inf] * n
    d[0] = 0
    nxts = deque([0])

    while nxts:
        u = nxts.popleft()

        for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

    return d bfs d = [inf] * n d [inf] * n [inf] inf n d[0] = 0 d[0] d 0 0 nxts = deque([0]) nxts deque([0]) deque [0] 0 while nxts:
        u = nxts.popleft()

        for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

     nxts u = nxts.popleft() u nxts.popleft() nxts.popleft nxts popleft for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

     v Adj[u] Adj u if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

     d[v] == inf d[v] d v inf d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 nxts.append(v) nxts.append nxts append v return d d n n Adj Adj if __name__ == '__main__':
    solve() __name__ == '__main__' __name__ '__main__' solve() solve inf = 1<<30 1<<30 inf def bfs(n, Adj):
    d = [inf] * n
    d[0] = 0
    nxts = deque([0])

    while nxts:
        u = nxts.popleft()

        for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

    return d def bfs(n, Adj):
    d = [inf] * n
    d[0] = 0
    nxts = deque([0])

    while nxts:
        u = nxts.popleft()

        for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

    return d bfs def solve():
    n = int(input())
    Adj = [None] * n

    for i in range(n):
        u, k, *line = [int(i) - 1 for i in input().split()]
        Adj[u] = line

    d = bfs(n, Adj)

    for i, di in enumerate(d, start=1):
        if di == inf:
            print(i, -1)
        else:
            print(i, di) def solve():
    n = int(input())
    Adj = [None] * n

    for i in range(n):
        u, k, *line = [int(i) - 1 for i in input().split()]
        Adj[u] = line

    d = bfs(n, Adj)

    for i, di in enumerate(d, start=1):
        if di == inf:
            print(i, -1)
        else:
            print(i, di) solve
import sys
from collections import deque

inf = 1<<30

def solve():
    n = int(input())
    Adj = [None] * n

    for i in range(n):
        u, k, *line = [int(i) - 1 for i in input().split()]
        Adj[u] = line

    d = bfs(n, Adj)

    for i, di in enumerate(d, start=1):
        if di == inf:
            print(i, -1)
        else:
            print(i, di)

def bfs(n, Adj):
    d = [inf] * n
    d[0] = 0
    nxts = deque([0])

    while nxts:
        u = nxts.popleft()

        for v in Adj[u]:
            if d[v] == inf:
                d[v] = d[u] + 1
                nxts.append(v)

    return d

if __name__ == '__main__':
    solve()