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 <queue>
#include <vector>
#include <iostream>
using namespace std;

// ------ Class ------ //
class Graph1 {
public:
	// ------ Variables ------ //
	static const int INF = 1000000000;
	int V, E; vector<vector<int> > G;

	// ------ Constructors ------ //
	Graph1() : V(0), E(0), G(vector<vector<int> >()) {}
	Graph1(int v) : V(v), E(0), G(vector<vector<int> >(v)) {}
	Graph1(vector<vector<int> > g) : V(g.size()), G(g) { for (int i = 0; i < g.size(); i++) E += g[i].size(); }

	// ------ Basic Functions ------ //
	int size() { return V; }
	void add1(int v1, int v2) { G[v1].push_back(v2); E++; }
	void add2(int v1, int v2) { add1(v1, v2); add1(v2, v1); }

	// ------ Operators ------ //
	friend bool operator==(const Graph1& g1, const Graph1& g2) { return g1.G == g2.G; }
	friend bool operator!=(const Graph1& g1, const Graph1& g2) { return g1.G != g2.G; }
	vector<int> operator[](int x) { return G[x]; }

	// ------ Algorithms ------ //
	vector<int> dist(int s) {
		queue<int> que; que.push(s);
		vector<int> d(V, INF); d[s] = 0;
		while (!que.empty()) {
			int u = que.front(); que.pop();
			for (int i = 0; i < G[u].size(); i++) {
				if (d[G[u][i]] == INF) {
					d[G[u][i]] = d[u] + 1;
					que.push(G[u][i]);
				}
			}
		}
		return d;
	}
};

// ------ Main ------ //
int n, u, k, v;
int main() {
	cin >> n; Graph1 g(n);
	for(int i = 0; i < n; i++) {
		cin >> u >> k;
		for(int j = 0; j < k; j++) cin >> v, g.add1(u - 1, v - 1);
	}
	vector<int> d = g.dist(0);
	for(int i = 0; i < n; i++) cout << i + 1 << ' ' << (d[i] != Graph1::INF ? d[i] : -1) << endl;
}

C++: #include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;


int main(){
  int n;
  cin >> n;
  vvll edge(n, vll(0));
  for(int i = 0; i < n; i++){
    ll u, k;
    cin >> u >> k;
    u--;
    for(int j = 0; j < k; j++){
      ll v;
      cin >> v;
      v--;
      edge.at(u).push_back(v);
    }
  }

  vector<Pll> d(n);
  for(int i = 0; i < n; i++){
    d.at(i).first = INF;
    d.at(i).second = (ll)i;
  }
  d.at(0).first = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(make_pair(0,0));

  while(!que.empty()){
    P p = que.top(); que.pop();

    if(d.at(p.second).first < p.first) continue;

    for(int i = 0; i < edge.at(p.second).size(); i++){
      ll v = edge.at(p.second).at(i);
      if(d.at(v).first > p.first + 1LL){
        d.at(v).first = p.first + 1LL;
        que.push(d.at(v));
      }  
    }
  }

  for(int i = 0; i < n; i++){
    if(i == 0) continue;
    if(d.at(i).first == INF) d.at(i).first = -1;
  }

  for(int i = 0; i < n; i++){
    cout << i+1 << ' ' << d.at(i).first << endl;
  }

}

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

int main(){
	int n;
	cin >> n;
	vector<vector<int> > to(n);
	rep(i,n){
		int u;
		cin >> u;
		u--;
		int k;
		cin >> k;
		rep(j,k){
			int v;
			cin >> v;
			v--;
			to[u].push_back(v);
		}
	}
	
	int dist[105];
	rep(i,n){
		dist[i] = -1;
	}
	
	queue<int> que;
	dist[0] = 0;
	que.push(0);
	while(!que.empty()){
		int p = que.front();
		que.pop();
		for (int i = 0; i < to[p].size(); ++i) {
			int nextv = to[p][i];
			if(dist[nextv] != -1) continue;
			dist[nextv] = dist[p] + 1;
			que.push(nextv);
		}
	}
	
	rep(i,n){
		cout << i+1 << " " << dist[i] << endl;
	}
    return 0;
}

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

int n, d[110];
bool a[110][110] = {};

void bfs(int u){
  
  queue<int> Q;
  
  Q.push(u);
  d[u] = 0;
  
  while(Q.size()){
    int p = Q.front();
    Q.pop();
    
    for(int i = 2;i <= n;i++){
      if(a[p][i] && d[i] == 0){
        Q.push(i);
        d[i] = d[p] + 1;
      }
    }
  }
}

int main(){
  
  int u, k, v;
  
  cin >> n;
  for(int i = 0;i < n;i++){
    cin >> u >> k;
    for(int j = 0;j < k;j++){
      cin >> v;
      a[u][v] = true;
    }
  }
  
  bfs(1);
  
  for(int i = 1;i <= n;i++){
    if(d[i] || i == 1) cout << i << " " << d[i] << endl;
    else cout << i << " " << -1 << endl;
  }
  
  return 0;
}

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

using namespace std;

const int N = 100;
const int Inf = 1e9;

int m[N][N];
int d[N];
int n;

void bfs(int);

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      m[i][j] = 0;
    }
    d[i] = -1;
  }

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

  return 0;
}

void bfs(int s) {
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < n; ++i) {
      if (m[u][i] == 0 || d[i] != -1) {
        continue;
      }
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << '\n';
  }
}