
def BFS(s = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
    if len(Q) != 0:
        BFS(Q[0])

n = int(raw_input())
A = [0] * n
for i in range(n):
    A[i] = [0] * n

for i in range(n):
    value = map(int, raw_input().split())
    u = value[0] - 1
    k = value[1]
    nodes = value[2:]
    for j in range(k):
        v = nodes[j] - 1
        A[u][v] = 1

color = [0] * n
Q = [0]
d = [-1] * n
d[0] = 0

BFS(0)

for i in range(n):
    print(str(i + 1) + " " + str(d[i])) def BFS(s = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
    if len(Q) != 0:
        BFS(Q[0]) BFS Q.pop(0) Q.pop Q pop 0 color[s] = 2 color[s] color s 2 for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
     j range(len(color)) range len(color) len color if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
     A[s][j] == 1 and color[j] == 0 A[s][j] == 1 A[s][j] [s] A s j 1 color[j] == 0 color[j] color j 0 Q.append(j) Q.append Q append j color[j] = 1 color[j] color j 1 d[j] = d[s] + 1 d[j] d j d[s] + 1 d[s] d s 1 if len(Q) != 0:
        BFS(Q[0]) len(Q) != 0 len(Q) len Q 0 BFS(Q[0]) BFS Q[0] Q 0 s = 0 s 0 n = int(raw_input()) n int(raw_input()) int raw_input() raw_input A = [0] * n A [0] * n [0] 0 n for i in range(n):
    A[i] = [0] * n i range(n) range n A[i] = [0] * n A[i] A i [0] * n [0] 0 n for i in range(n):
    value = map(int, raw_input().split())
    u = value[0] - 1
    k = value[1]
    nodes = value[2:]
    for j in range(k):
        v = nodes[j] - 1
        A[u][v] = 1 i range(n) range n value = map(int, raw_input().split()) value map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split u = value[0] - 1 u value[0] - 1 value[0] value 0 1 k = value[1] k value[1] value 1 nodes = value[2:] nodes value[2:] value 2: 2 for j in range(k):
        v = nodes[j] - 1
        A[u][v] = 1 j range(k) range k v = nodes[j] - 1 v nodes[j] - 1 nodes[j] nodes j 1 A[u][v] = 1 A[u][v] [u] A u v 1 color = [0] * n color [0] * n [0] 0 n Q = [0] Q [0] 0 d = [-1] * n d [-1] * n [-1] -1 n d[0] = 0 d[0] d 0 0 BFS(0) BFS 0 for i in range(n):
    print(str(i + 1) + " " + str(d[i])) i range(n) range n print(str(i + 1) + " " + str(d[i])) print str(i + 1) + " " + str(d[i]) str(i + 1) + " " str(i + 1) str i + 1 i 1 " " str(d[i]) str d[i] d i A = [0] * n [0] * n A k = value[1] value[1] k n = int(raw_input()) int(raw_input()) n u = value[0] - 1 value[0] - 1 u d = [-1] * n [-1] * n d nodes = value[2:] value[2:] nodes v = nodes[j] - 1 nodes[j] - 1 v color = [0] * n [0] * n color def BFS(s = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
    if len(Q) != 0:
        BFS(Q[0]) def BFS(s = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
    if len(Q) != 0:
        BFS(Q[0]) BFS Q = [0] [0] Q value = map(int, raw_input().split()) map(int, raw_input().split()) value

def BFS(s = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
    if len(Q) != 0:
        BFS(Q[0])

n = int(raw_input())
A = [0] * n
for i in range(n):
    A[i] = [0] * n

for i in range(n):
    value = map(int, raw_input().split())
    u = value[0] - 1
    k = value[1]
    nodes = value[2:]
    for j in range(k):
        v = nodes[j] - 1
        A[u][v] = 1

color = [0] * n
Q = [0]
d = [-1] * n
d[0] = 0

BFS(0)

for i in range(n):
    print(str(i + 1) + " " + str(d[i]))