from collections import deque
import sys


def bfs():
    color[0] = "GRAY"
    d[0] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = "BLACK"


if __name__ == "__main__":
    n = int(sys.stdin.readline())
    M = [[0] * n for i in range(n)]
    color = ["WHITE"] * n
    d = [-1] * n
    Q = deque()
    for inp in sys.stdin.readlines():
        inp = list(map(int, inp.split()))
        for i in inp[2:]:
            M[inp[0]-1][i-1] = 1
    bfs()
    for i in range(n):
        print(i+1, d[i]) from collections import deque import sys sys def bfs():
    color[0] = "GRAY"
    d[0] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = "BLACK" 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 range(n):
            if M[u][v] 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 range(n):
            if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
         v range(n) range n if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
         M[u][v] and color[v] == "WHITE" M[u][v] [u] M u v 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" if __name__ == "__main__":
    n = int(sys.stdin.readline())
    M = [[0] * n for i in range(n)]
    color = ["WHITE"] * n
    d = [-1] * n
    Q = deque()
    for inp in sys.stdin.readlines():
        inp = list(map(int, inp.split()))
        for i in inp[2:]:
            M[inp[0]-1][i-1] = 1
    bfs()
    for i in range(n):
        print(i+1, d[i]) __name__ == "__main__" __name__ "__main__" n = int(sys.stdin.readline()) n int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline [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)] color = ["WHITE"] * n color ["WHITE"] * n ["WHITE"] "WHITE" n d = [-1] * n d [-1] * n [-1] -1 n Q = deque() Q deque() deque for inp in sys.stdin.readlines():
        inp = list(map(int, inp.split()))
        for i in inp[2:]:
            M[inp[0]-1][i-1] = 1
     inp sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines inp = list(map(int, inp.split())) inp list(map(int, inp.split())) list map(int, inp.split()) map int inp.split() inp.split inp split for i in inp[2:]:
            M[inp[0]-1][i-1] = 1
     i inp[2:] inp 2: 2 M[inp[0]-1][i-1] = 1 M[inp[0]-1][i-1] [inp[0]-1] M inp[0]-1 inp[0] inp 0 1 i-1 i 1 1 bfs() bfs for i in range(n):
        print(i+1, d[i]) i range(n) range n print(i+1, d[i]) print i+1 i 1 d[i] d i color = ["WHITE"] * n ["WHITE"] * n color n = int(sys.stdin.readline()) int(sys.stdin.readline()) n d = [-1] * n [-1] * n d Q = deque() deque() Q def bfs():
    color[0] = "GRAY"
    d[0] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = "BLACK" def bfs():
    color[0] = "GRAY"
    d[0] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = "BLACK" bfs M = [[0] * n for i in range(n)] [[0] * n for i in range(n)] M inp = list(map(int, inp.split())) list(map(int, inp.split())) inp
from collections import deque
import sys


def bfs():
    color[0] = "GRAY"
    d[0] = 0
    Q.append(0)
    while len(Q) != 0:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and color[v] == "WHITE":
                color[v] = "GRAY"
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = "BLACK"


if __name__ == "__main__":
    n = int(sys.stdin.readline())
    M = [[0] * n for i in range(n)]
    color = ["WHITE"] * n
    d = [-1] * n
    Q = deque()
    for inp in sys.stdin.readlines():
        inp = list(map(int, inp.split()))
        for i in inp[2:]:
            M[inp[0]-1][i-1] = 1
    bfs()
    for i in range(n):
        print(i+1, d[i])