Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

C++: #include<bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define int long long
#define vec(a,n) vector<int> (a)((n));
#define Vec(a,n) vector<string> (a)((n));
#define twovec(a,n,m) vector<vector<int>> a(n,vector<int>(m,0));
#define Twovec(a,n,m) vector<vector<double>> a(n,vector<double>(m,0));
#define P pair<int,int>
#define All(a) (a).begin(),(a).end()
#define Sort(a) sort(All(a));
#define Reverse(a) reverse(All(a));
#define PQ(n) priority_queue<P,vector<P>,greater<P>> (n)
#define pq(n) priority_queue<int> (n)
#define print(a) cout << (a) << endl
#define printD(a) cout << setprecision(15) << (a) << endl;
using namespace std;
int max_int = 2147483647;
int prime = 10e9+7;
void Debug(auto a);
int nibul(auto a,auto b);
int nibuu(auto a,auto b);
void input(vector<auto>& a,int n);
double PI = 3.14159265358979;
int n;

struct Edge{
    int from,to,cost;
    bool operator<(const Edge& another) const{
        return cost < another.cost;
    }
    bool operator>(const Edge& another) const{
        return cost > another.cost;
    }
};

class dGrapf{
    vector<vector<P>> grapf;
    vector<int> d;
    bool loop = false;
    public:
    dGrapf(int n){
        this->grapf.resize(n);
        this->d.resize(grapf.size(),max_int);
    }
    dGrapf(){
        this->grapf.resize(10e6);
        this->d.resize(grapf.size(),max_int);
    }
    /* 辺の元、先、コストの順
     */
    void edge(int parent,int child,int weight=1){
        P p = P(child,weight);
        grapf[parent].push_back(p);
    }
    void dijkstra(int n){
        d[n] = 0;
        PQ(que);
        que.emplace(0,n);
        while(!que.empty()){
            P p = que.top();
            que.pop();
            int v = p.second;
            if(d[v] < p.first) continue;
            for(P e: grapf[v]){
                if(d[e.first] > d[v]+e.second){
                    d[e.first] = d[v]+e.second;
                    que.emplace(d[e.first],e.first);
                }
            }
        }
    }
    void bellmanford(int n){
        fill(d.begin(), d.end(), max_int);
        d[n] = 0;
        bool update = true;
        int cnt = 0;
        while(update){
            update = false;
            REP(i,grapf.size()){
                REP(j,grapf[i].size()){
                    if(d[i] != max_int && d[grapf[i][j].first] > d[i] + grapf[i][j].second){
                        d[grapf[i][j].first] = d[i] + grapf[i][j].second;
                        update = true;
                    }
                }
            }
            cnt++;
            if(cnt > grapf.size()){
                loop = true;
                return;
            }
        }
    }
    vector<int> topological_sort(){
        vector<int> indeg(grapf.size(),0);
        REP(i,grapf.size()){
            for(auto v:grapf[i]){
                indeg[v.first]++;
            }
        }
        queue<int> que;
        REP(i,indeg.size()){
            if(indeg[i] == 0){
                que.push(i);
            }
        }
        vector<int> ans;
        while(!que.empty()){
            int u = que.front();
            que.pop();
            ans.push_back(u);
            for(auto v:grapf[u]){
                indeg[v.first]--;
                if(indeg[v.first] == 0) que.push(v.first);
            }
        }
        if(ans.size() != grapf.size()) loop = true;
        return ans;
    }

    void print_AOJ(){
        REP(i,d.size()){
            if(d[i] == max_int){
                cout << i+1 << " -1\n";
            }
            else{
                cout << i+1 << " " << d[i] << endl;
            }
        }
    }
    void De(){
        REP(i,grapf.size()){
            cout << "{";
            for(P p:grapf[i]){
                cout << "{" << p.first << "," << p.second << "}";
            }
            cout << "}\n";
        }
        Debug(d);
    }
};


signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n;
    dGrapf g(n);
    REP(i,n){
        int u,k;
        cin >> u >> k;
        u--;
        REP(j,k){
            int tmp;
            cin >> tmp;
            tmp--;
            g.edge(u,tmp);
        }
    }
    g.dijkstra(0);
    g.print_AOJ();
    return 0;
}

void Debug(auto a){
    cout << "{ ";
    for(auto b: a){
        cout << b << " ";
    }
    cout << "}" << endl;
}

//key以上の初めてのitr
int nibul(auto a,auto b){return lower_bound(All(a),b) - a.begin();}
//key以下の最後のitr
int nibuu(auto a,auto b){return upper_bound(All(a),b) - a.begin()-1;}

void input(vector<auto>& a,int n){
    REP(i,n){
        cin >> a[i];
    }
}