n = int(raw_input())
INF = 10000000
queue = []

def bfs(s):
    queue.append(s)
    d = [INF for i in range(n)]
    d[s] = 0
    while len(queue) != 0:
        u = queue.pop(0)
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
    for i in range(n):
        if d[i] == INF:
            length = -1
        else:
            length = d[i]
        print "%d %d" %(i + 1, length)
        
M = [[0 for j in range(n)] for i in range(n)]
for i in range(n):
    tmp = map(int, raw_input().split())
    u = tmp[0]
    k = tmp[1]
    u -= 1
    for j in range(k):
        v = tmp[j + 2]
        v -= 1
        M[u][v] = 1
bfs(0) n = int(raw_input()) n int(raw_input()) int raw_input() raw_input INF = 10000000 INF 10000000 queue = [] queue [] def bfs(s):
    queue.append(s)
    d = [INF for i in range(n)]
    d[s] = 0
    while len(queue) != 0:
        u = queue.pop(0)
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
    for i in range(n):
        if d[i] == INF:
            length = -1
        else:
            length = d[i]
        print "%d %d" %(i + 1, length)
         bfs queue.append(s) queue.append queue append s INF for i in range(n) for i in range(n) i range(n) range n for i in range(n) INF d = [INF for i in range(n)] d [INF for i in range(n)] d[s] = 0 d[s] d s 0 while len(queue) != 0:
        u = queue.pop(0)
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
     len(queue) != 0 len(queue) len queue 0 u = queue.pop(0) u queue.pop(0) queue.pop queue pop 0 for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
     v range(n) range n if M[u][v] == 0:
                continue
             M[u][v] == 0 M[u][v] [u] M u v 0 continue if d[v] != INF:
                continue
             d[v] != INF d[v] d v INF continue d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 queue.append(v) queue.append queue append v for i in range(n):
        if d[i] == INF:
            length = -1
        else:
            length = d[i]
        print "%d %d" %(i + 1, length)
         i range(n) range n if d[i] == INF:
            length = -1
        else:
            length = d[i]
         d[i] == INF d[i] d i INF length = -1 length -1 length = d[i] length d[i] d i print "%d %d" %(i + 1, length) "%d %d" (i + 1, length) i + 1 i 1 length s s [0 for j in range(n)] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0 for j in range(n)] M = [[0 for j in range(n)] for i in range(n)] M [[0 for j in range(n)] for i in range(n)] for i in range(n):
    tmp = map(int, raw_input().split())
    u = tmp[0]
    k = tmp[1]
    u -= 1
    for j in range(k):
        v = tmp[j + 2]
        v -= 1
        M[u][v] = 1 i range(n) range n tmp = map(int, raw_input().split()) tmp map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split u = tmp[0] u tmp[0] tmp 0 k = tmp[1] k tmp[1] tmp 1 u -= 1 u 1 for j in range(k):
        v = tmp[j + 2]
        v -= 1
        M[u][v] = 1 j range(k) range k v = tmp[j + 2] v tmp[j + 2] tmp j + 2 j 2 v -= 1 v 1 M[u][v] = 1 M[u][v] [u] M u v 1 bfs(0) bfs 0 INF = 10000000 10000000 INF u = tmp[0] tmp[0] u v -= 1 1 v M = [[0 for j in range(n)] for i in range(n)] [[0 for j in range(n)] for i in range(n)] M v = tmp[j + 2] tmp[j + 2] v n = int(raw_input()) int(raw_input()) n tmp = map(int, raw_input().split()) map(int, raw_input().split()) tmp u -= 1 1 u k = tmp[1] tmp[1] k queue = [] [] queue def bfs(s):
    queue.append(s)
    d = [INF for i in range(n)]
    d[s] = 0
    while len(queue) != 0:
        u = queue.pop(0)
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
    for i in range(n):
        if d[i] == INF:
            length = -1
        else:
            length = d[i]
        print "%d %d" %(i + 1, length)
         def bfs(s):
    queue.append(s)
    d = [INF for i in range(n)]
    d[s] = 0
    while len(queue) != 0:
        u = queue.pop(0)
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
    for i in range(n):
        if d[i] == INF:
            length = -1
        else:
            length = d[i]
        print "%d %d" %(i + 1, length)
         bfs
n = int(raw_input())
INF = 10000000
queue = []

def bfs(s):
    queue.append(s)
    d = [INF for i in range(n)]
    d[s] = 0
    while len(queue) != 0:
        u = queue.pop(0)
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != INF:
                continue
            d[v] = d[u] + 1
            queue.append(v)
    for i in range(n):
        if d[i] == INF:
            length = -1
        else:
            length = d[i]
        print "%d %d" %(i + 1, length)
        
M = [[0 for j in range(n)] for i in range(n)]
for i in range(n):
    tmp = map(int, raw_input().split())
    u = tmp[0]
    k = tmp[1]
    u -= 1
    for j in range(k):
        v = tmp[j + 2]
        v -= 1
        M[u][v] = 1
bfs(0)