from collections import deque
n = int(raw_input())

M = [[0]*n for _ in xrange(n)]
color = [0] * n
WHITE = 0
GRAY = 1
BLACK = 2
d = [-1] * n
Q = deque([])

def bfs():
    color[0] = GRAY
    d[0] = 0
    Q.append(0)
    while len(Q) > 0:
        u = Q.popleft()
        for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = BLACK
    for u in xrange(n):
        print "{} {}".format(u+1, d[u])
    return

def main():
    for i in xrange(n):
        a = map(int, raw_input().split())
        u = a[0]-1
        k = a[1]
        for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
    bfs()
    return 0

main() from collections import deque n = int(raw_input()) n int(raw_input()) int raw_input() raw_input [0]*n for _ in xrange(n) for _ in xrange(n) _ xrange(n) xrange n for _ in xrange(n) [0]*n [0] 0 n M = [[0]*n for _ in xrange(n)] M [[0]*n for _ in xrange(n)] color = [0] * n color [0] * n [0] 0 n WHITE = 0 WHITE 0 GRAY = 1 GRAY 1 BLACK = 2 BLACK 2 d = [-1] * n d [-1] * n [-1] -1 n Q = deque([]) Q deque([]) deque [] def bfs():
    color[0] = GRAY
    d[0] = 0
    Q.append(0)
    while len(Q) > 0:
        u = Q.popleft()
        for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = BLACK
    for u in xrange(n):
        print "{} {}".format(u+1, d[u])
    return bfs color[0] = GRAY color[0] color 0 GRAY d[0] = 0 d[0] d 0 0 Q.append(0) Q.append Q append 0 while len(Q) > 0:
        u = Q.popleft()
        for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = BLACK
     len(Q) > 0 len(Q) len Q 0 u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
         v xrange(n) xrange n if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
         M[u][v] == 1 and color[v] == WHITE M[u][v] == 1 M[u][v] [u] M u v 1 color[v] == WHITE color[v] color v WHITE color[v] = GRAY color[v] color v GRAY d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 Q.append(v) Q.append Q append v color[u] = BLACK color[u] color u BLACK for u in xrange(n):
        print "{} {}".format(u+1, d[u])
     u xrange(n) xrange n print "{} {}".format(u+1, d[u]) "{} {}".format "{} {}" format u+1 u 1 d[u] d u return def main():
    for i in xrange(n):
        a = map(int, raw_input().split())
        u = a[0]-1
        k = a[1]
        for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
    bfs()
    return 0 main for i in xrange(n):
        a = map(int, raw_input().split())
        u = a[0]-1
        k = a[1]
        for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
     i xrange(n) xrange n a = map(int, raw_input().split()) a map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split u = a[0]-1 u a[0]-1 a[0] a 0 1 k = a[1] k a[1] a 1 for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
     j xrange(k) xrange k v = a[j+2] -  1 v a[j+2] -  1 a[j+2] a j+2 j 2 1 M[u][v] = 1 M[u][v] [u] M u v 1 bfs() bfs return 0 0 main() main Q = deque([]) deque([]) Q WHITE = 0 0 WHITE n = int(raw_input()) int(raw_input()) n color = [0] * n [0] * n color BLACK = 2 2 BLACK def main():
    for i in xrange(n):
        a = map(int, raw_input().split())
        u = a[0]-1
        k = a[1]
        for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
    bfs()
    return 0 def main():
    for i in xrange(n):
        a = map(int, raw_input().split())
        u = a[0]-1
        k = a[1]
        for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
    bfs()
    return 0 main M = [[0]*n for _ in xrange(n)] [[0]*n for _ in xrange(n)] M GRAY = 1 1 GRAY def bfs():
    color[0] = GRAY
    d[0] = 0
    Q.append(0)
    while len(Q) > 0:
        u = Q.popleft()
        for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = BLACK
    for u in xrange(n):
        print "{} {}".format(u+1, d[u])
    return def bfs():
    color[0] = GRAY
    d[0] = 0
    Q.append(0)
    while len(Q) > 0:
        u = Q.popleft()
        for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = BLACK
    for u in xrange(n):
        print "{} {}".format(u+1, d[u])
    return bfs d = [-1] * n [-1] * n d
from collections import deque
n = int(raw_input())

M = [[0]*n for _ in xrange(n)]
color = [0] * n
WHITE = 0
GRAY = 1
BLACK = 2
d = [-1] * n
Q = deque([])

def bfs():
    color[0] = GRAY
    d[0] = 0
    Q.append(0)
    while len(Q) > 0:
        u = Q.popleft()
        for v in xrange(n):
            if M[u][v] == 1 and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = BLACK
    for u in xrange(n):
        print "{} {}".format(u+1, d[u])
    return

def main():
    for i in xrange(n):
        a = map(int, raw_input().split())
        u = a[0]-1
        k = a[1]
        for j in xrange(k):
            v = a[j+2] -  1
            M[u][v] = 1
    
    bfs()
    return 0

main()