# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
# Breadth First Search
# Result:
import sys

class Graph(object):
    def __init__(self, size):
        super(Graph, self).__init__()
        self.__size = size
        self.__vertices = [None] * size

    def add_vertex(self, vertex):
        self.__vertices[vertex.id_ - 1] = vertex

    def get_vertex(self, vid):
        return self.__vertices[vid - 1]

    def breadth_first_search(self):
        ds = [-1] * self.__size
        vs = [None] * self.__size
        vs[0] = [self.get_vertex(1)]
        ds[0] = 0
        vs_idx = 0
        while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
        for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

    def __str__(self):
        str_ = ''
        for e in self.__vertices:
            str_ += '%s\n' % e
        return str_

class Vertex(object):
    def __init__(self, id_):
        super(Vertex, self).__init__()
        self.id_ = id_
        self.adjacents = []
        self.next_idx = 0

    def add_adjacent(self, avid):
        self.adjacents.append(avid)

    def __str__(self):
        str_ = '%d ' % self.id_
        if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
        return str_



s = int(sys.stdin.readline().strip())
graph = Graph(s)
for _ in range(0, s):
    vals = [int(x) for x in sys.stdin.readline().strip().split(' ')]
    v = Vertex(vals[0])
    graph.add_vertex(v)
    if vals[1] == 0: continue
    for i in range(0, vals[1]):
        v.add_adjacent(vals[2 + i])

graph.breadth_first_search() import sys sys class Graph(object):
    def __init__(self, size):
        super(Graph, self).__init__()
        self.__size = size
        self.__vertices = [None] * size

    def add_vertex(self, vertex):
        self.__vertices[vertex.id_ - 1] = vertex

    def get_vertex(self, vid):
        return self.__vertices[vid - 1]

    def breadth_first_search(self):
        ds = [-1] * self.__size
        vs = [None] * self.__size
        vs[0] = [self.get_vertex(1)]
        ds[0] = 0
        vs_idx = 0
        while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
        for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

    def __str__(self):
        str_ = ''
        for e in self.__vertices:
            str_ += '%s\n' % e
        return str_ Graph def __init__(self, size):
        super(Graph, self).__init__()
        self.__size = size
        self.__vertices = [None] * size

     __init__ super(Graph, self).__init__() (Graph, self).__init__ (Graph, self) super Graph self __init__ self.__size = size self.__size self __size size self.__vertices = [None] * size self.__vertices self __vertices [None] * size [None] None size self self size size def add_vertex(self, vertex):
        self.__vertices[vertex.id_ - 1] = vertex

     add_vertex self.__vertices[vertex.id_ - 1] = vertex self.__vertices[vertex.id_ - 1] self.__vertices self __vertices vertex.id_ - 1 vertex.id_ vertex id_ 1 vertex self self vertex vertex def get_vertex(self, vid):
        return self.__vertices[vid - 1]

     get_vertex return self.__vertices[vid - 1] self.__vertices[vid - 1] self.__vertices self __vertices vid - 1 vid 1 self self vid vid def breadth_first_search(self):
        ds = [-1] * self.__size
        vs = [None] * self.__size
        vs[0] = [self.get_vertex(1)]
        ds[0] = 0
        vs_idx = 0
        while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
        for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

     breadth_first_search ds = [-1] * self.__size ds [-1] * self.__size [-1] -1 self.__size self __size vs = [None] * self.__size vs [None] * self.__size [None] None self.__size self __size vs[0] = [self.get_vertex(1)] vs[0] vs 0 [self.get_vertex(1)] self.get_vertex(1) self.get_vertex self get_vertex 1 ds[0] = 0 ds[0] ds 0 0 vs_idx = 0 vs_idx 0 while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
         vs_idx < self.__size and vs[vs_idx] != None vs_idx < self.__size vs_idx self.__size self __size vs[vs_idx] != None vs[vs_idx] vs vs_idx None vlst = vs[vs_idx] vlst vs[vs_idx] vs vs_idx vs_idx += 1 vs_idx 1 for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
         vtx vlst for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
         advid vtx.adjacents vtx adjacents adv = self.get_vertex(advid) adv self.get_vertex(advid) self.get_vertex self get_vertex advid if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
         ds[advid - 1] == -1 ds[advid - 1] ds advid - 1 advid 1 -1 ds[advid - 1] = vs_idx ds[advid - 1] ds advid - 1 advid 1 vs_idx if vs[vs_idx] == None:
                            vs[vs_idx] = []
                         vs[vs_idx] == None vs[vs_idx] vs vs_idx None vs[vs_idx] = [] vs[vs_idx] vs vs_idx [] vs[vs_idx].append(adv) [vs_idx].append [vs_idx] vs vs_idx append adv for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

     idx d enumerate(ds) enumerate ds print '%d %d' % ((idx + 1), d) '%d %d' ((idx + 1), d) idx + 1 idx 1 d self self def __str__(self):
        str_ = ''
        for e in self.__vertices:
            str_ += '%s\n' % e
        return str_ __str__ str_ = '' str_ '' for e in self.__vertices:
            str_ += '%s\n' % e
         e self.__vertices self __vertices str_ += '%s\n' % e str_ '%s\n' % e '%s\n' e return str_ str_ self self object class Vertex(object):
    def __init__(self, id_):
        super(Vertex, self).__init__()
        self.id_ = id_
        self.adjacents = []
        self.next_idx = 0

    def add_adjacent(self, avid):
        self.adjacents.append(avid)

    def __str__(self):
        str_ = '%d ' % self.id_
        if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
        return str_ Vertex def __init__(self, id_):
        super(Vertex, self).__init__()
        self.id_ = id_
        self.adjacents = []
        self.next_idx = 0

     __init__ super(Vertex, self).__init__() (Vertex, self).__init__ (Vertex, self) super Vertex self __init__ self.id_ = id_ self.id_ self id_ id_ self.adjacents = [] self.adjacents self adjacents [] self.next_idx = 0 self.next_idx self next_idx 0 self self id_ id_ def add_adjacent(self, avid):
        self.adjacents.append(avid)

     add_adjacent self.adjacents.append(avid) self.adjacents.append self.adjacents self adjacents append avid self self avid avid def __str__(self):
        str_ = '%d ' % self.id_
        if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
        return str_ __str__ str_ = '%d ' % self.id_ str_ '%d ' % self.id_ '%d ' self.id_ self id_ if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
         len(self.adjacents) == 0 len(self.adjacents) len self.adjacents self adjacents 0 str_ += '0' str_ '0' str_ += '%d' % len(self.adjacents) str_ '%d' % len(self.adjacents) '%d' len(self.adjacents) len self.adjacents self adjacents for e in self.adjacents:
                str_ += ' %d' % e
         e self.adjacents self adjacents str_ += ' %d' % e str_ ' %d' % e ' %d' e return str_ str_ self self object s = int(sys.stdin.readline().strip()) s int(sys.stdin.readline().strip()) int sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip graph = Graph(s) graph Graph(s) Graph s for _ in range(0, s):
    vals = [int(x) for x in sys.stdin.readline().strip().split(' ')]
    v = Vertex(vals[0])
    graph.add_vertex(v)
    if vals[1] == 0: continue
    for i in range(0, vals[1]):
        v.add_adjacent(vals[2 + i]) _ range(0, s) range 0 s int(x) for x in sys.stdin.readline().strip().split(' ') for x in sys.stdin.readline().strip().split(' ') x sys.stdin.readline().strip().split(' ') sys.stdin.readline().strip().split sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip split ' ' for x in sys.stdin.readline().strip().split(' ') int(x) int x vals = [int(x) for x in sys.stdin.readline().strip().split(' ')] vals [int(x) for x in sys.stdin.readline().strip().split(' ')] v = Vertex(vals[0]) v Vertex(vals[0]) Vertex vals[0] vals 0 graph.add_vertex(v) graph.add_vertex graph add_vertex v if vals[1] == 0: continue
     vals[1] == 0 vals[1] vals 1 0 for i in range(0, vals[1]):
        v.add_adjacent(vals[2 + i]) i range(0, vals[1]) range 0 vals[1] vals 1 v.add_adjacent(vals[2 + i]) v.add_adjacent v add_adjacent vals[2 + i] vals 2 + i 2 i graph.breadth_first_search() graph.breadth_first_search graph breadth_first_search class Graph(object):
    def __init__(self, size):
        super(Graph, self).__init__()
        self.__size = size
        self.__vertices = [None] * size

    def add_vertex(self, vertex):
        self.__vertices[vertex.id_ - 1] = vertex

    def get_vertex(self, vid):
        return self.__vertices[vid - 1]

    def breadth_first_search(self):
        ds = [-1] * self.__size
        vs = [None] * self.__size
        vs[0] = [self.get_vertex(1)]
        ds[0] = 0
        vs_idx = 0
        while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
        for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

    def __str__(self):
        str_ = ''
        for e in self.__vertices:
            str_ += '%s\n' % e
        return str_ class Graph(object):
    def __init__(self, size):
        super(Graph, self).__init__()
        self.__size = size
        self.__vertices = [None] * size

    def add_vertex(self, vertex):
        self.__vertices[vertex.id_ - 1] = vertex

    def get_vertex(self, vid):
        return self.__vertices[vid - 1]

    def breadth_first_search(self):
        ds = [-1] * self.__size
        vs = [None] * self.__size
        vs[0] = [self.get_vertex(1)]
        ds[0] = 0
        vs_idx = 0
        while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
        for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

    def __str__(self):
        str_ = ''
        for e in self.__vertices:
            str_ += '%s\n' % e
        return str_ Graph class Vertex(object):
    def __init__(self, id_):
        super(Vertex, self).__init__()
        self.id_ = id_
        self.adjacents = []
        self.next_idx = 0

    def add_adjacent(self, avid):
        self.adjacents.append(avid)

    def __str__(self):
        str_ = '%d ' % self.id_
        if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
        return str_ class Vertex(object):
    def __init__(self, id_):
        super(Vertex, self).__init__()
        self.id_ = id_
        self.adjacents = []
        self.next_idx = 0

    def add_adjacent(self, avid):
        self.adjacents.append(avid)

    def __str__(self):
        str_ = '%d ' % self.id_
        if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
        return str_ Vertex v = Vertex(vals[0]) Vertex(vals[0]) v graph = Graph(s) Graph(s) graph vals = [int(x) for x in sys.stdin.readline().strip().split(' ')] [int(x) for x in sys.stdin.readline().strip().split(' ')] vals s = int(sys.stdin.readline().strip()) int(sys.stdin.readline().strip()) s
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C
# Breadth First Search
# Result:
import sys

class Graph(object):
    def __init__(self, size):
        super(Graph, self).__init__()
        self.__size = size
        self.__vertices = [None] * size

    def add_vertex(self, vertex):
        self.__vertices[vertex.id_ - 1] = vertex

    def get_vertex(self, vid):
        return self.__vertices[vid - 1]

    def breadth_first_search(self):
        ds = [-1] * self.__size
        vs = [None] * self.__size
        vs[0] = [self.get_vertex(1)]
        ds[0] = 0
        vs_idx = 0
        while vs_idx < self.__size and vs[vs_idx] != None:
            vlst = vs[vs_idx]
            vs_idx += 1
            for vtx in vlst:
                for advid in vtx.adjacents:
                    adv = self.get_vertex(advid)
                    if ds[advid - 1] == -1:
                        ds[advid - 1] = vs_idx
                        if vs[vs_idx] == None:
                            vs[vs_idx] = []
                        vs[vs_idx].append(adv)
        for idx, d in enumerate(ds):
            print '%d %d' % ((idx + 1), d)

    def __str__(self):
        str_ = ''
        for e in self.__vertices:
            str_ += '%s\n' % e
        return str_

class Vertex(object):
    def __init__(self, id_):
        super(Vertex, self).__init__()
        self.id_ = id_
        self.adjacents = []
        self.next_idx = 0

    def add_adjacent(self, avid):
        self.adjacents.append(avid)

    def __str__(self):
        str_ = '%d ' % self.id_
        if len(self.adjacents) == 0:
            str_ += '0'
        else:
            str_ += '%d' % len(self.adjacents)
            for e in self.adjacents:
                str_ += ' %d' % e
        return str_



s = int(sys.stdin.readline().strip())
graph = Graph(s)
for _ in range(0, s):
    vals = [int(x) for x in sys.stdin.readline().strip().split(' ')]
    v = Vertex(vals[0])
    graph.add_vertex(v)
    if vals[1] == 0: continue
    for i in range(0, vals[1]):
        v.add_adjacent(vals[2 + i])

graph.breadth_first_search()