Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define lper(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[4]={1,0,-1,0};
const ll dx[4]={0,-1,0,1};
const ll ddx[8]={ 0, 1, 0,-1, 1, 1,-1,-1};
const ll ddy[8]={ 1, 0,-1, 0, 1,-1, 1,-1};
ll modfac(ll a){
    ll ans=1;
    while(a>1){
        ans*=a;
        ans%=1000000007;
        a--;
    }
    return ans;
}
ll modinv(ll a,ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}
struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) {}

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};
ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n;cin >> n;
    vvl g(n);
    rep(i,n){
        ll a,b;cin >> a >>b;
        a--;
        rep(j,b){
            ll c;cin >> c;
            g[a].pb(c-1);
        }
    }
    queue<ll> que;
    que.push(0);
    vl dist(n,-1);
    dist[0]=0;
    while(!que.empty()){
        ll v=que.front();
        que.pop();
        for(auto nv:g[v]){
            if(dist[nv]!=-1)continue;
            dist[nv]=dist[v]+1;
            que.push(nv);
        }
    }
    rep(i,n)cout << i+1 <<" "<<dist[i] << endl;
}

C++: #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), i##_max = (b); i < i##_max; ++i)
#define RFOR(i, a, b) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;
using vi = vector<int>;
using vd = vector<double>;
using i64 = int64_t;

constexpr int INF = 1 << 30;
constexpr int MOD = 1000000007;

template <typename T>
void print(vector<T> &v) {
  REP(i, v.size()) {
    if (i) cout << " ";
    cout << v[i];
  }
  cout << endl;
}

void dfs(vector<vi> &M, vi &d, int s) {
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    REP(v, d.size()) {
      if (M[u][v] == 0) continue;
      if (d[v] != INF) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  REP(i, d.size()) cout << i + 1 << " " << ((d[i] == INF) ? -1 : d[i]) << endl;
}

int main() {
  int n, u, k, v;
  cin >> n;
  vector<vi> M(n, vi(n, 0));
  vi d(n, INF);
  REP(i, n) {
    cin >> u >> k;
    REP(j, k) {
      cin >> v;
      M[u - 1][v - 1] = 1;
    }
  }
  dfs(M, d, 0);
}

C++: import java.util.LinkedList;
import java.util.Scanner;



public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
		AdjListGraph g = new AdjListGraph(size, true);
		for (int i = 0; i < size; i++){
			int v = sc.nextInt()-1;
			int d = sc.nextInt();
			for(int j=0; j<d; j++){
				g.addEdge(v, sc.nextInt()-1);
			}
		}
		g.BFS(0);
	}

}


class Pair{
	int key;int value;
	Pair(int K, int V){key = K; value = V;}
	int getKey(){return key;}
	int getValue(){return value;}
}	

class AdjListGraph { // http://theoryofprogramming.com/adjacency-list-in-java/
	private boolean digraph;
	private final LinkedList<Pair>[] adjList;
	@SuppressWarnings("unchecked")
	AdjListGraph(int numV, boolean flag){
		adjList = (LinkedList<Pair>[]) new LinkedList[numV];
		for (int i = 0; i < adjList.length; i++) {
			adjList[i] = new LinkedList<>();
		}
		digraph = flag;
	}
	boolean directed(){return digraph;} //return the graph is directed or not
	int getNumOfVertices(){return adjList.length;} //return the num of vertices
	public void addEdge(int startV, int endV, int weight) {
		adjList[startV].add(new Pair(endV, weight));
		if(!digraph){adjList[endV].add(new Pair(startV, weight));}
	} //add an edge to the graph. 
	public void addEdge(int startV, int endV) {
		addEdge(startV, endV, 1); 
	} //weight is set to 1 as a default
	public LinkedList<Integer> getEdgesFromVertexNoWeight(int startV) {
		LinkedList< Integer > edgeList= (LinkedList< Integer >) new LinkedList<Integer>();
		for(Pair pair : adjList[startV]){
			edgeList.add(pair.getKey());
		}
		return edgeList;
	} // get the list of the edges leaving startV. This is for no weight graph. 
	public void print() {
		int i = 0;
		for (LinkedList<Pair> list : adjList) {
			System.out.print("adjList[" + i + "] -> ");
			for (Pair edge : list) {System.out.print(edge.getKey() + "(" + edge.getValue() + ")");}
			i++;
			System.out.println();
		}
	}
	void BFS(int src){
		boolean visited[] = new boolean[getNumOfVertices()];		
		int dist[] = new int[getNumOfVertices()];
		LinkedList<Integer> queue = new LinkedList<Integer>();
		visited[src]=true;	
		queue.add(src);	
		while (queue.size() != 0){
			int tmp = queue.poll();
			LinkedList<Integer> neighbors =  getEdgesFromVertexNoWeight(tmp);
			for(Integer i : neighbors){
				if(!visited[i]){
					visited[i] = true;
					dist[i] = dist[tmp]+1;
					queue.add(i);
				}
			}
		}
		for(int i=0; i< dist.length ; i++){
			if(i!=src && dist[i]==0){
				dist[i]=-1;
			}
		}
		for(int i=0; i< dist.length ; i++){
			System.out.println(Integer.toString(i+1) + " " +  dist[i]);
		}
//		System.out.println(Arrays.toString(dist));
	}
}