from collections import deque

def main():
    n = int(input())
    _next = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next[u] = v
    queue = deque()
    queue.append(0)
    d = [-1] * n
    d[0] = 0
    while queue:
        u = queue.popleft()
        for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
    for i, v in enumerate(d, start=1):
        print(i, v)


if __name__ == '__main__':
    main()
 from collections import deque def main():
    n = int(input())
    _next = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next[u] = v
    queue = deque()
    queue.append(0)
    d = [-1] * n
    d[0] = 0
    while queue:
        u = queue.popleft()
        for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
    for i, v in enumerate(d, start=1):
        print(i, v) main n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] _next = [[] for _ in range(n)] _next [[] for _ in range(n)] for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next[u] = v
     _ range(n) range n u, k, *v = map(lambda s: int(s) - 1, input().split()) u map(lambda s: int(s) - 1, input().split()) map lambda s: int(s) - 1 int(s) - 1 int(s) int s 1 s input().split() ().split () input split k *v v _next[u] = v _next[u] _next u v queue = deque() queue deque() deque queue.append(0) queue.append queue append 0 d = [-1] * n d [-1] * n [-1] -1 n d[0] = 0 d[0] d 0 0 while queue:
        u = queue.popleft()
        for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
     queue u = queue.popleft() u queue.popleft() queue.popleft queue popleft for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
     v _next[u] _next u if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
     d[v] == -1 d[v] d v -1 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, v in enumerate(d, start=1):
        print(i, v) i v enumerate(d, start=1) enumerate d start=1 start 1 print(i, v) print i v if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n = int(input())
    _next = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next[u] = v
    queue = deque()
    queue.append(0)
    d = [-1] * n
    d[0] = 0
    while queue:
        u = queue.popleft()
        for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
    for i, v in enumerate(d, start=1):
        print(i, v) def main():
    n = int(input())
    _next = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next[u] = v
    queue = deque()
    queue.append(0)
    d = [-1] * n
    d[0] = 0
    while queue:
        u = queue.popleft()
        for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
    for i, v in enumerate(d, start=1):
        print(i, v) main
from collections import deque

def main():
    n = int(input())
    _next = [[] for _ in range(n)]
    for _ in range(n):
        u, k, *v = map(lambda s: int(s) - 1, input().split())
        _next[u] = v
    queue = deque()
    queue.append(0)
    d = [-1] * n
    d[0] = 0
    while queue:
        u = queue.popleft()
        for v in _next[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                queue.append(v)
    for i, v in enumerate(d, start=1):
        print(i, v)


if __name__ == '__main__':
    main()

