Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

int main(){
    int n;
    cin >> n;

    Graph G(n);
    for(int i = 0;i < n; i++){
        int u, k;
        cin >> u >> k;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            G[u-1].push_back(v-1);
        }
    }

    vector<int> dist(n, -1);
    queue<int> que;

    dist[0] = 0;
    que.push(0);

    while(!que.empty()){
        int v = que.front();
        que.pop();

        for(int nv: G[v]){
            if(dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    for(int i = 0; i < n; i++) cout << i+1 << ' ' << dist[i] << endl;
}

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

using namespace std;

void bfs(vector<vector<int>>& g, vector<int>& dist) {
    queue<int> q;
    vector<bool> visited(g.size(), false);
    q.push(1);
    dist[1] = 0;
    while (!q.empty()) {
        int idx = q.front();
        q.pop();
        visited[idx] = true;

        for (int i = 0; i < g[idx].size(); ++i) {
            int adj = g[idx][i];
            if (!visited[adj]) {
                q.push(adj);
                if (dist[adj] != -1)
                    dist[adj] = min(dist[adj], dist[idx] + 1);
                else
                    dist[adj] = dist[idx] + 1;
            }
        }
    }
}

int main() {
    int n;
    cin >> n;

    vector<vector<int>> g(n + 1);
    for (int i = 1; i <= n; ++i) {
        int u, k;
        cin >> u >> k;

        for (int j = 0; j < k; ++j) {
            int v;
            cin >> v;
            g[i].push_back(v);
        }
    }

    vector<int> dist(n + 1, -1);
    bfs(g, dist);

    for (int i = 1; i <= n; ++i)
        cout << i << ' ' << dist[i] << '\n';
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
vbl vis;
lli p;
vll dp;

lli solve(lli x){
    if(x == 1) return dp[x] = 0;
    if(dp[x]) return dp[x];
    lli ret = -1;
    for(lli i = 0;i < g[x].size();i++){
        ret = solve(g[x][i]) < 0 ? ret : ret < 0 ? solve(g[x][i]) + 1 : min(ret,solve(g[x][i])+1);
    }
    return dp[x] = ret;
}

int main(){
    cin >> n;
    g = mat(n+1);
    dp = vll(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[v].push_back(u);
        }
    }
    for(lli i = 1;i <= n;i++){
        cout << i << " " << solve(i) << endl;
    }
    return 0;
}

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

using namespace std;

int D[100100];//始点からの距離を入れる

int g[1001][1001]={{0}};//要素を0で初期化
void bfs(int src,int n){//srcは開始位置この場合だと頂点1だから0
  // cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()  ){
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for(int dst= 0; dst < n; dst++){
      if(g[cur][dst]==1 && D[dst]==0){
        D[dst]=D[cur]+1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n){
  cout << 1 <<" "<< 0 <<endl;
  for (int i = 1; i < n; ++i)
  {
    if(D[i]==0){
      cout << i+1 << " "<< -1 <<endl;
    }else{
      cout << i+1 <<" "<< D[i]<<endl;
    }
  }

}

int main(){
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n,a,b,c;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> a >> b ;
    for (int i = 0; i < b; ++i)
    {
      cin >> c;
      g[a-1][c-1]=1;/* code */
    }
  }
  bfs(0,n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }


}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
	int n;
	cin >> n;
	vector<vector<int>> g(n);
	rep(i, n) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(j, k) {
			int v;
			cin >> v;
			v--;
			g[u].push_back(v);
		}
	}
	vector<int> d(n, -1);
	d[0] = 0;
	queue<int> q;
	q.push(0);
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto nv : g[v]) {
			if (d[nv] != -1) continue;
			d[nv] = d[v] + 1;
			q.push(nv);
		}
	}
	rep(i, n) cout << i + 1 << " " << d[i] << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

const int MAX_V = 101;
vector<int> adj[MAX_V];
bool used[MAX_V];
int id[MAX_V];

void bfs(int s){
  queue<int> que;
  que.push(s);
  used[s]=true;
  id[s]=0;
  for(;!que.empty();){
    int p=que.front();
    que.pop();
    for(int i:adj[p]){
      if(!used[i]){
	que.push(i);
	used[i]=true;
	id[i]=id[p]+1;
      }
    }
  }
}

int main(){
  fill(id,id+MAX_V,-1);
  int n;cin>>n;
  REP(i,n){
    int u,k,v;cin>>u>>k;
    REP(j,k){
      cin>>v;
      adj[u].push_back(v);
    }
  }
  bfs(1);
  REP(i,n) cout<<i+1<<" "<<id[i+1]<<endl;
}