import sys
import math
from collections import defaultdict, deque
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]

def main():
    N = I()
    mylist = [[] for i in range(N)]
    for i in range(N):
        temp = LI()
        temp2 = temp[1]
        for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
    d = [-1] * N
    d[0] = 0
    dq = deque()
    dq.append(0)
    while dq:
        now = dq.popleft()
        for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
    for index, num in enumerate(d):
            print(index+1, num)
            
        
    
if __name__ == "__main__":
    main() import sys sys import math math from collections import defaultdict, deque input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def RD(): return input().rstrip() RD def F(): return float(input().rstrip()) F def I(): return int(input().rstrip()) I def MI(): return map(int, input().split()) MI def MF(): return map(float,input().split()) MF def LI(): return list(map(int, input().split())) LI def LF(): return list(map(float,input().split())) LF [num for i in range(W)] for j in range(H) for j in range(H) j range(H) range H for j in range(H) [num for i in range(W)] def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] Init H H W W num num def main():
    N = I()
    mylist = [[] for i in range(N)]
    for i in range(N):
        temp = LI()
        temp2 = temp[1]
        for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
    d = [-1] * N
    d[0] = 0
    dq = deque()
    dq.append(0)
    while dq:
        now = dq.popleft()
        for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
    for index, num in enumerate(d):
            print(index+1, num)
            
        
     main N = I() N I() I [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] mylist = [[] for i in range(N)] mylist [[] for i in range(N)] for i in range(N):
        temp = LI()
        temp2 = temp[1]
        for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
     i range(N) range N temp = LI() temp LI() LI temp2 = temp[1] temp2 temp[1] temp 1 for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
     j range(temp2) range temp2 mylist[temp[0]-1].append(temp[2+j]-1) [temp[0]-1].append [temp[0]-1] mylist temp[0]-1 temp[0] temp 0 1 append temp[2+j]-1 temp[2+j] temp 2+j 2 j 1 d = [-1] * N d [-1] * N [-1] -1 N d[0] = 0 d[0] d 0 0 dq = deque() dq deque() deque dq.append(0) dq.append dq append 0 while dq:
        now = dq.popleft()
        for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
     dq now = dq.popleft() now dq.popleft() dq.popleft dq popleft for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
     next_p mylist[now] mylist now if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
     d[next_p] == -1 d[next_p] d next_p -1 d[next_p] = d[now]+1 d[next_p] d next_p d[now]+1 d[now] d now 1 dq.append(next_p) dq.append dq append next_p for index, num in enumerate(d):
            print(index+1, num)
            
        
     index num enumerate(d) enumerate d print(index+1, num) print index+1 index 1 num if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI input = sys.stdin.readline sys.stdin.readline input def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] Init def MI(): return map(int, input().split()) def MI(): return map(int, input().split()) MI def LF(): return list(map(float,input().split())) def LF(): return list(map(float,input().split())) LF def MF(): return map(float,input().split()) def MF(): return map(float,input().split()) MF def RD(): return input().rstrip() def RD(): return input().rstrip() RD def F(): return float(input().rstrip()) def F(): return float(input().rstrip()) F def main():
    N = I()
    mylist = [[] for i in range(N)]
    for i in range(N):
        temp = LI()
        temp2 = temp[1]
        for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
    d = [-1] * N
    d[0] = 0
    dq = deque()
    dq.append(0)
    while dq:
        now = dq.popleft()
        for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
    for index, num in enumerate(d):
            print(index+1, num)
            
        
     def main():
    N = I()
    mylist = [[] for i in range(N)]
    for i in range(N):
        temp = LI()
        temp2 = temp[1]
        for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
    d = [-1] * N
    d[0] = 0
    dq = deque()
    dq.append(0)
    while dq:
        now = dq.popleft()
        for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
    for index, num in enumerate(d):
            print(index+1, num)
            
        
     main def I(): return int(input().rstrip()) def I(): return int(input().rstrip()) I
import sys
import math
from collections import defaultdict, deque
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]

def main():
    N = I()
    mylist = [[] for i in range(N)]
    for i in range(N):
        temp = LI()
        temp2 = temp[1]
        for j in range(temp2):
            mylist[temp[0]-1].append(temp[2+j]-1)
    #初期化,距離、訪れたか。
    d = [-1] * N
    d[0] = 0
    dq = deque()
    dq.append(0)
    while dq:
        now = dq.popleft()
        for next_p in mylist[now]:
            if d[next_p] == -1:
                d[next_p] = d[now]+1
                dq.append(next_p)
    for index, num in enumerate(d):
            print(index+1, num)
            
        
    
if __name__ == "__main__":
    main()
