Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int n;
struct indata {
    int now; int dist;
};

int main() {
    cin >> n;
    vector< vector<int> > graph(n, vector<int>());
    for(int i = 0; i < n; ++i) {
        int u, k;
        cin >> u >> k;
        u--;
        for(int j = 0; j < k; ++j) {
            int v;
            cin >> v;
            v--;
            graph.at(u).push_back(v);
            // graph.at(v).push_back(u);
        }
    }
    
    indata first;
    first.now = 0; first.dist = 0;
    queue<indata> que;
    que.push(first);

    vector<bool> visited(n, false);
    vector<int> distgraph(n, 1e9);

    while(!que.empty()) {
        indata exeq = que.front();
        que.pop();
        visited.at(exeq.now) = true;
        chmin(distgraph.at(exeq.now), exeq.dist);
        for(int i = 0; i < graph.at(exeq.now).size(); ++i) {
            if(!visited.at(graph.at(exeq.now).at(i))) {
                indata next;
                next.now = graph.at(exeq.now).at(i);
                next.dist = exeq.dist + 1;
                que.push(next);
            }
        }
    }

    for(int i = 0; i < n; ++i) {
        if(distgraph.at(i) == 1e9) {
            cout << i+1 << " " << -1 << endl;
        }else {
            cout << i+1 << " " << distgraph.at(i) << endl;
        }
    }
}

C++: #include<iostream>
#include<queue>
using namespace std;
int N;
int D[101];
void bfs(int src){
    int u,k,a,d[101][101];
    bool visit[101];
    for(int i=1;i<=N;++i){
        cin>>u>>k;
        for(int j=0;j<k;++j){
            cin>>a;
            d[u][a]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        if(i!=1&&D[i]==0){
            D[i]=-1;
        }
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int k[1000][1000], n, a, b, c, t, x[1000];
queue<int>Q;
queue<int>R;

int main() {
	memset(x, -1, sizeof(x));
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		for (int j = 0; j < b; j++) {
			cin >> c;
			k[i][c] = 1;
		}
	}
	Q.push(1);
	x[1] = 0;
	while (!Q.empty()) {
		t++;
		while (!Q.empty()) {
			for (int i = 1; i <= n; i++) {
				if (x[i] == -1) {
					x[i] = t;
					R.push(i);
				}
			}
			Q.pop();
		}
		while (!R.empty()) {
			Q.push(R.front());
			R.pop();
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << i << ' ' << x[i] << endl;
	}
	return 0;
}

C++: 
#include <utility>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

int D[110]; // ?????¢????¨???¶
int v, A[100][100], u, k, w;
queue<int> Q;

void bfs(int src) {

	Q.push(src);
	D[src] = 0;

	while (!Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int i = 0; i < v; ++i) {
			if (A[cur][i] == 1 && D[i] == 0) {
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}

	for (int l = 0; l < v; ++l) {
		if (D[l] == 0 && l != 0) { D[l] = -1; }
		cout << l+1 << " " << D[l] << endl;
	}

}

int main(){
	cin >> v;
	// ???????????§??????v
	for (int i = 0; i < v; ++i) {

		cin >> u >> k;
		// ??????????????????u???k?????\?????????????????°??????

		for (int j = 0; j < k; ++j) {

			cin >> w;
			A[u - 1][w - 1] = 1;

			// ??£??\???????????§?¨??????????????????????????????????

		}
	}

	
	bfs(0);

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
static const int INFTY = (1 << 21);

class Edge{
public:
    int from, to, cost;
    Edge(int from = 0, int to = 0, int cost = 0) :
            from(from), to(to), cost(cost){}

    bool operator < (const Edge &e) const{
        return cost < e.cost;
    }
    bool operator >(const Edge &e) const{
        return cost > e.cost;
    }

};

class Node{
public:
    vector<Edge> edges;
    int dist;

    Node(){
        dist = INFTY;
    }
};

class Graph{
public:
    vector<Node> nodes;
    Graph(int n) {
        rep(i, n) { Node n;  nodes.push_back(n); }
    }
    Node& operator[](int i){
        return nodes[i];
    }
};

class Data{

public:
    int dist;
    Edge *edge;
    Data(int dist, Edge *edge) {
        this->dist = dist;
        this->edge = edge;
    }

    bool operator < (const Data &d) const{
        return dist < d.dist;
    }
    bool operator >(const Data &d) const{
        return dist > d.dist;
    }
};

Graph G(0);
int V;

int main() {

    cin >> V;
    G = Graph(V);

    rep(i, V){
        int from; cin >> from; from--;
        int e_count; cin >> e_count;

        vint vec;
        rep(j, e_count){
            int to; cin >> to; to--;
            vec.push_back(to);
        }
        sort(vec.begin(), vec.end());
        rep(j, e_count){
            G[from].edges.push_back(Edge(from, vec[j], 1));
        }
    }

    // bfs
    queue<Data> q;
    Edge e(-1, 0, 0);
    q.push(Data(0, &e));
    while(!q.empty()){
        Data d = q.front(); q.pop();
        Edge &e = *(d.edge);
        Node &n = G[e.to];

        if(n.dist != INFTY) continue;
        n.dist = d.dist;

        for(auto &ee: n.edges){
            q.push(Data(n.dist+1, &ee));
        }
    }

    rep(i, V) {
        if (G[i].dist == INFTY) G[i].dist = -1;
    }

    rep(i, V){
        fprintf(stdout, "%d %d\n", i+1, G[i].dist);
    }
}