from collections import deque

WHITE = 0
GRAY = 1
BLACK = 2

n = int(input())

M = [[0] * n for i in range(n)]

d = [-1] * (n)
color = [WHITE] * (n)


time = 0

def dfs_visit(u):
    global time
    time += 1
    color[u] = GRAY
    d[u] = time
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(v)
    color[u] = BLACK
    time += 1

def bfs(s):
    queue = deque([])
    queue.append(s)
    d[s] = 0
    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                continue
            d[v] = d[u] + 1
            queue.append(v)

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


if __name__ == "__main__":
    for i in range(n):
        arr = list(map(int, input().split()))
        for v in (arr[2:]):
            M[arr[0]-1][v-1] = 1

    bfs(0)
 from collections import deque WHITE = 0 WHITE 0 GRAY = 1 GRAY 1 BLACK = 2 BLACK 2 n = int(input()) n int(input()) int input() input [0] * n for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0] * n [0] 0 n M = [[0] * n for i in range(n)] M [[0] * n for i in range(n)] d = [-1] * (n) d [-1] * (n) [-1] -1 n color = [WHITE] * (n) color [WHITE] * (n) [WHITE] WHITE n time = 0 time 0 def dfs_visit(u):
    global time
    time += 1
    color[u] = GRAY
    d[u] = time
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(v)
    color[u] = BLACK
    time += 1 dfs_visit global time time += 1 time 1 color[u] = GRAY color[u] color u GRAY d[u] = time d[u] d u time for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(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 color[v] == WHITE:
            dfs_visit(v)
     color[v] == WHITE color[v] color v WHITE dfs_visit(v) dfs_visit v color[u] = BLACK color[u] color u BLACK time += 1 time 1 u u def bfs(s):
    queue = deque([])
    queue.append(s)
    d[s] = 0
    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                continue
            d[v] = d[u] + 1
            queue.append(v)

    for i in range(n):
        print("%d %d" % (i+1, d[i])) bfs queue = deque([]) queue deque([]) deque [] queue.append(s) queue.append queue append s d[s] = 0 d[s] d s 0 while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                continue
            d[v] = d[u] + 1
            queue.append(v)

     len(queue) > 0 len(queue) len queue 0 u = queue.popleft() u queue.popleft() queue.popleft queue popleft for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                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] != -1:
                continue
             d[v] != -1 d[v] d v -1 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):
        print("%d %d" % (i+1, d[i])) i range(n) range n print("%d %d" % (i+1, d[i])) print "%d %d" % (i+1, d[i]) "%d %d" (i+1, d[i]) i+1 i 1 d[i] d i s s if __name__ == "__main__":
    for i in range(n):
        arr = list(map(int, input().split()))
        for v in (arr[2:]):
            M[arr[0]-1][v-1] = 1

    bfs(0) __name__ == "__main__" __name__ "__main__" for i in range(n):
        arr = list(map(int, input().split()))
        for v in (arr[2:]):
            M[arr[0]-1][v-1] = 1

     i range(n) range n arr = list(map(int, input().split())) arr list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for v in (arr[2:]):
            M[arr[0]-1][v-1] = 1

     v arr[2:] arr 2: 2 M[arr[0]-1][v-1] = 1 M[arr[0]-1][v-1] [arr[0]-1] M arr[0]-1 arr[0] arr 0 1 v-1 v 1 1 bfs(0) bfs 0 M = [[0] * n for i in range(n)] [[0] * n for i in range(n)] M time = 0 0 time def dfs_visit(u):
    global time
    time += 1
    color[u] = GRAY
    d[u] = time
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(v)
    color[u] = BLACK
    time += 1 def dfs_visit(u):
    global time
    time += 1
    color[u] = GRAY
    d[u] = time
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(v)
    color[u] = BLACK
    time += 1 dfs_visit BLACK = 2 2 BLACK n = int(input()) int(input()) n GRAY = 1 1 GRAY WHITE = 0 0 WHITE arr = list(map(int, input().split())) list(map(int, input().split())) arr color = [WHITE] * (n) [WHITE] * (n) color d = [-1] * (n) [-1] * (n) d def bfs(s):
    queue = deque([])
    queue.append(s)
    d[s] = 0
    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                continue
            d[v] = d[u] + 1
            queue.append(v)

    for i in range(n):
        print("%d %d" % (i+1, d[i])) def bfs(s):
    queue = deque([])
    queue.append(s)
    d[s] = 0
    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                continue
            d[v] = d[u] + 1
            queue.append(v)

    for i in range(n):
        print("%d %d" % (i+1, d[i])) bfs
from collections import deque

WHITE = 0
GRAY = 1
BLACK = 2

n = int(input())

M = [[0] * n for i in range(n)]

d = [-1] * (n)
color = [WHITE] * (n)


time = 0

def dfs_visit(u):
    global time
    time += 1
    color[u] = GRAY
    d[u] = time
    for v in range(n):
        if M[u][v] == 0:
            continue
        if color[v] == WHITE:
            dfs_visit(v)
    color[u] = BLACK
    time += 1

def bfs(s):
    queue = deque([])
    queue.append(s)
    d[s] = 0
    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if M[u][v] == 0:
                continue
            if d[v] != -1:
                continue
            d[v] = d[u] + 1
            queue.append(v)

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


if __name__ == "__main__":
    for i in range(n):
        arr = list(map(int, input().split()))
        for v in (arr[2:]):
            M[arr[0]-1][v-1] = 1

    bfs(0)

