# https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C

from collections import deque


class Data:
    def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth


if __name__ == "__main__":
    n = int(input())
    d = {}
    for _ in range(n):
        u, k, *li = list(map(int, input().split()))
        d[u] = li

    queue = deque()

    result = {}
    for i in range(n):
        result[i+1] = -1

    depth = 0
    queue = deque()
    queue.appendleft(Data(index=1, depth=0))
    while queue:
        data = queue.pop()
        index = data.index
        depth = data.depth
        if result[index] != -1:
            continue
        result[index] = depth

        for child in d[index]:
            queue.appendleft(Data(index=child, depth=depth+1))

    for k, v in result.items():
        print(k, v)
 from collections import deque class Data:
    def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth Data def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth __init__ super().__init__() ().__init__ () super __init__ self.index = index self.index self index index self.depth = depth self.depth self depth depth self self index index depth depth if __name__ == "__main__":
    n = int(input())
    d = {}
    for _ in range(n):
        u, k, *li = list(map(int, input().split()))
        d[u] = li

    queue = deque()

    result = {}
    for i in range(n):
        result[i+1] = -1

    depth = 0
    queue = deque()
    queue.appendleft(Data(index=1, depth=0))
    while queue:
        data = queue.pop()
        index = data.index
        depth = data.depth
        if result[index] != -1:
            continue
        result[index] = depth

        for child in d[index]:
            queue.appendleft(Data(index=child, depth=depth+1))

    for k, v in result.items():
        print(k, v) __name__ == "__main__" __name__ "__main__" n = int(input()) n int(input()) int input() input d = {} d {} for _ in range(n):
        u, k, *li = list(map(int, input().split()))
        d[u] = li

     _ range(n) range n u, k, *li = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split k *li li d[u] = li d[u] d u li queue = deque() queue deque() deque result = {} result {} for i in range(n):
        result[i+1] = -1

     i range(n) range n result[i+1] = -1 result[i+1] result i+1 i 1 -1 depth = 0 depth 0 queue = deque() queue deque() deque queue.appendleft(Data(index=1, depth=0)) queue.appendleft queue appendleft Data(index=1, depth=0) Data index=1 index 1 depth=0 depth 0 while queue:
        data = queue.pop()
        index = data.index
        depth = data.depth
        if result[index] != -1:
            continue
        result[index] = depth

        for child in d[index]:
            queue.appendleft(Data(index=child, depth=depth+1))

     queue data = queue.pop() data queue.pop() queue.pop queue pop index = data.index index data.index data index depth = data.depth depth data.depth data depth if result[index] != -1:
            continue
         result[index] != -1 result[index] result index -1 continue result[index] = depth result[index] result index depth for child in d[index]:
            queue.appendleft(Data(index=child, depth=depth+1))

     child d[index] d index queue.appendleft(Data(index=child, depth=depth+1)) queue.appendleft queue appendleft Data(index=child, depth=depth+1) Data index=child index child depth=depth+1 depth depth+1 depth 1 for k, v in result.items():
        print(k, v) k v result.items() result.items result items print(k, v) print k v class Data:
    def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth class Data:
    def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth Data k, *li = list(map(int, input().split())) list(map(int, input().split())) k result = {} {} result n = int(input()) int(input()) n depth = 0 0 depth queue = deque() deque() queue u, k, *li = list(map(int, input().split())) list(map(int, input().split())) u queue = deque() deque() queue index = data.index data.index index d = {} {} d data = queue.pop() queue.pop() data depth = data.depth data.depth depth
# https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C

from collections import deque


class Data:
    def __init__(self, index, depth):
        super().__init__()
        self.index = index
        self.depth = depth


if __name__ == "__main__":
    n = int(input())
    d = {}
    for _ in range(n):
        u, k, *li = list(map(int, input().split()))
        d[u] = li

    queue = deque()

    result = {}
    for i in range(n):
        result[i+1] = -1

    depth = 0
    queue = deque()
    queue.appendleft(Data(index=1, depth=0))
    while queue:
        data = queue.pop()
        index = data.index
        depth = data.depth
        if result[index] != -1:
            continue
        result[index] = depth

        for child in d[index]:
            queue.appendleft(Data(index=child, depth=depth+1))

    for k, v in result.items():
        print(k, v)

