import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n = I()
ukv = [LI() for _ in range(n)]
graph = {i:collections.deque() for i in range(1,n+1)} #1_indexed
for x in ukv:
    u,k,*v = x
    for y in v:
        graph[u].append(y)
seen = [0]*(n+1) #1_indexed
min_dist = [-1]*(n+1) #1_indexed
queue = collections.deque() #頂点NOを入れておく
def bfs():
    seen[1] = 1
    queue.append(1)
    min_dist[1] = 0
    while queue:
        q_NO = queue.popleft()
        q_dist = min_dist[q_NO]
        if not graph[q_NO]:
            continue
        g = graph[q_NO]
        for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1
bfs()
for i,ans in enumerate(min_dist[1:],1):
    print(i,ans) import collections,sys collections sys def I(): return int(sys.stdin.readline().rstrip()) I def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) LI n = I() n I() I LI() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI() LI ukv = [LI() for _ in range(n)] ukv [LI() for _ in range(n)] i:collections.deque() for i in range(1,n+1) for i in range(1,n+1) i range(1,n+1) range 1 n+1 n 1 for i in range(1,n+1) i collections.deque() collections.deque collections deque graph = {i:collections.deque() for i in range(1,n+1)} graph {i:collections.deque() for i in range(1,n+1)} for x in ukv:
    u,k,*v = x
    for y in v:
        graph[u].append(y) x ukv u,k,*v = x u x k *v v for y in v:
        graph[u].append(y) y v graph[u].append(y) [u].append [u] graph u append y seen = [0]*(n+1) seen [0]*(n+1) [0] 0 n+1 n 1 min_dist = [-1]*(n+1) min_dist [-1]*(n+1) [-1] -1 n+1 n 1 queue = collections.deque() queue collections.deque() collections.deque collections deque def bfs():
    seen[1] = 1
    queue.append(1)
    min_dist[1] = 0
    while queue:
        q_NO = queue.popleft()
        q_dist = min_dist[q_NO]
        if not graph[q_NO]:
            continue
        g = graph[q_NO]
        for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1 bfs seen[1] = 1 seen[1] seen 1 1 queue.append(1) queue.append queue append 1 min_dist[1] = 0 min_dist[1] min_dist 1 0 while queue:
        q_NO = queue.popleft()
        q_dist = min_dist[q_NO]
        if not graph[q_NO]:
            continue
        g = graph[q_NO]
        for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1 queue q_NO = queue.popleft() q_NO queue.popleft() queue.popleft queue popleft q_dist = min_dist[q_NO] q_dist min_dist[q_NO] min_dist q_NO if not graph[q_NO]:
            continue
         not graph[q_NO] graph[q_NO] graph q_NO continue g = graph[q_NO] g graph[q_NO] graph q_NO for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1 _ range(len(g)) range len(g) len g g_NO = g.popleft() g_NO g.popleft() g.popleft g popleft if seen[g_NO]:
                continue
             seen[g_NO] seen g_NO continue seen[g_NO] = 1 seen[g_NO] seen g_NO 1 queue.append(g_NO) queue.append queue append g_NO min_dist[g_NO] = q_dist+1 min_dist[g_NO] min_dist g_NO q_dist+1 q_dist 1 bfs() bfs for i,ans in enumerate(min_dist[1:],1):
    print(i,ans) i ans enumerate(min_dist[1:],1) enumerate min_dist[1:] min_dist 1: 1 1 print(i,ans) print i ans k,*v = x x k def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) LI min_dist = [-1]*(n+1) [-1]*(n+1) min_dist def I(): return int(sys.stdin.readline().rstrip()) def I(): return int(sys.stdin.readline().rstrip()) I queue = collections.deque() collections.deque() queue u,k,*v = x x u seen = [0]*(n+1) [0]*(n+1) seen n = I() I() n ukv = [LI() for _ in range(n)] [LI() for _ in range(n)] ukv def bfs():
    seen[1] = 1
    queue.append(1)
    min_dist[1] = 0
    while queue:
        q_NO = queue.popleft()
        q_dist = min_dist[q_NO]
        if not graph[q_NO]:
            continue
        g = graph[q_NO]
        for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1 def bfs():
    seen[1] = 1
    queue.append(1)
    min_dist[1] = 0
    while queue:
        q_NO = queue.popleft()
        q_dist = min_dist[q_NO]
        if not graph[q_NO]:
            continue
        g = graph[q_NO]
        for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1 bfs graph = {i:collections.deque() for i in range(1,n+1)} {i:collections.deque() for i in range(1,n+1)} graph
import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n = I()
ukv = [LI() for _ in range(n)]
graph = {i:collections.deque() for i in range(1,n+1)} #1_indexed
for x in ukv:
    u,k,*v = x
    for y in v:
        graph[u].append(y)
seen = [0]*(n+1) #1_indexed
min_dist = [-1]*(n+1) #1_indexed
queue = collections.deque() #頂点NOを入れておく
def bfs():
    seen[1] = 1
    queue.append(1)
    min_dist[1] = 0
    while queue:
        q_NO = queue.popleft()
        q_dist = min_dist[q_NO]
        if not graph[q_NO]:
            continue
        g = graph[q_NO]
        for _ in range(len(g)):
            g_NO = g.popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            queue.append(g_NO)
            min_dist[g_NO] = q_dist+1
bfs()
for i,ans in enumerate(min_dist[1:],1):
    print(i,ans)
