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<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
  queue<int> Q;
  int n,u,k,vv,cur,dst;
  cin >> n;
  vector<int> D(n,-1),check(n,0);
  vector< vector<int> > v(n);
  for(int i = 0;i < n;++i){
    cin >> u >> k;
    for(int j = 0;j < k;++j){
      cin >> vv;
      v[u - 1].push_back(vv);
    }
    sort(v[u - 1].begin(),v[u - 1].end());
  }
  D[0] = 0;
  check[0] = 1;
  Q.push(1);
  while(!Q.empty()){
    cur = Q.front();
    Q.pop();
    for(int i = 0;i < v[cur - 1].size();++i){
      dst = v[cur - 1][i];
      if(check[dst - 1] == 0){
	D[dst - 1] = D[cur - 1] + 1;
	check[dst - 1] = 1;
	Q.push(dst);
      }
    }
  }
  for(int i = 0;i < n;++i)
    cout << i + 1 << " " << D[i] << endl;
}

C++: #include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#include <list>
#define inf 200
using namespace std;

int n;
list<int> adj[101];
int d[101];
bool f[101];
void BFS(int v)
{
    int m = 0;
    f[v] = false;
    while (!adj[v].empty())
    {
        d[adj[v].front()] = min(d[v] + 1, d[adj[v].front()]);
        adj[v].pop_front();
    }
    for (int i = 1; i <= n; i++)
    {
        if (f[i] && d[i] < d[m])
            m = i;
    }
    if (m > 0)
        BFS(m);
}

void print()
{
    for (int i = 1; i <= n; i++)
    {
        if (d[i] == inf)
            d[i] = -1;
        printf("%d %d\n", i, d[i]);
    }
}

int main()
{
    int u, k, v;
    cin >> n;
    for (int i = 0; i <= n; i++)
    {
        d[i] = inf;
        f[i] = true;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> u >> k;
        while (k > 0)
        {
            cin >> v;
            adj[u].push_back(v);
            k--;
        }
    }
    d[1] = 0;
    BFS(1);

    print();

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int INF=(1<<21);
int n,M[101][101];
int d[101]; //最短距離を記録(到達不可な時はINF)
void bfs(int s){
  queue<int>q;
  q.push(s);//始点sをいれる(訪問)
  for(int i=0;i<n;i++) d[i]=INF;
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0) continue; //辺がない
      if(d[v]!=INF) continue; //訪問したことがある
      d[v]=d[u]+1; //vは未訪問の頂点であり、隣接しているためd[u]に1を足す
      q.push(v); //vを始点に
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  } //到達不可能か
}
int main(){
  int u,k,v;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    u--;
    for(int j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}

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

using ll = long long;
#define REP(i, n) for(int (i)=0; (i)< (n); ++i)
#define REPR(i, n) for(int (i)=(n); (i)>=0; --i)
#define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i)


int main(){
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  REP(i, n){
    int id, k;
    cin >> id >> k;
    g[i].resize(k);
    REP(j, k){
      int v;
      cin >> v;
      v--;
      g[i].push_back(v);
    }
  }
  vector<int> dist(n, -1);
  dist[0] = 0;
  queue<pair<int, int>> que;
  que.push({0,0});
  while(!que.empty()){
    int v, d;
    tie(v, d) = que.front();que.pop();
    REP(i, g[v].size()){
      int nv = g[v][i];
      if(dist[nv] != -1) continue;
      dist[nv] = d + 1;
      que.push({nv, d+1});
    }
  }
  REP(i, n){
    cout << i+1 << " " << dist[i] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <random>
#include <cstdlib>
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

vector<int> V[105];
queue<int> Q;
vector<int> color(105, 1), d(105, 0);

int main() {
	int n;
	cin >> n;
	Loop(i, n) {
		int u, k;
		cin >> u >> k;
		Loop(j, k) {
			int v;
			cin >> v;
			V[u].push_back(v);
		}
	}
	//
	Q.push(1);
	color[1] = 2;
	while (Q.size() > 0) {
		int u = Q.front();
		Q.pop();
		Loop(i, V[u].size()) {
			if (color[V[u][i]] == 1) {
				Q.push(V[u][i]);
				color[V[u][i]] = 2;
				d[V[u][i]] = d[u] + 1;
			}
		}
	}
	Loop1(i, n) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

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

int n;
vector< vector<int> > G;
vector<int> color;
vector<int> dis;

void Bfs(int start) {
  queue<int> q;
  q.push(start);
  color[start]++;
  dis[start] = 0;

  while (q.empty() == false) {
    int u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (G[u][v] == 1 && color[v] == 0) {
        color[v]++;
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
    color[u]++;
  }
}

int main() {
  cin >> n;
  G.resize(n, vector<int>(n));
  color.resize(n);
  dis.resize(n, -1);
  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--;
      G[u][v]++;
    }
  }

  Bfs(0);

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