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 <queue>

using ll = long long;
using namespace std;
int n, A[100][100];
int d[100];
/* initialize */

void bfs(int s){
    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; ++i) d[i] = 1<<30;
    d[s] = 0;
    int u;
    while(!q.empty()){
        u = q.front(); q.pop();
        for (int i = 0; i < n; ++i) {
            if ( A[u][i] == 0 ) continue;
            if (d[i] != 1<<30) continue;
            d[i] = d[u] + 1;
            q.push(i);
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << i+1 << " " << ( (d[i] == 1<<30) ? -1 : d[i]) << "\n";
    }
}
/* main */
int main() {
    int u, k, v;
    cin >> n;
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) A[i][j] = 0;
    for (int i = 0; i < n; ++i) {
        cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            cin >> v;
            A[u-1][v-1] = 1;
        }
    }
    bfs(0);
    return 0;
}

C++: // C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

Int N;
vector<vector<Int> > G(101);
vector<Int> D(101, -1);

void input() {
  cin >> N;
  Int u, c, v;
  rep(N) {
    cin >> u >> c;u--;
    rep(c) {
      cin >> v;v--;
      G.at(u).push_back(v);
    }
  }
}

void bfs() {
  queue<Int> Q;
  Q.push(0);
  D.at(0) = 0;
  while (Q.size()) {
    Int u = Q.front();Q.pop();
    for (auto v: G.at(u)) {
      if (D.at(v) != -1) continue;
      Q.push(v);
      D.at(v) = D.at(u) + 1;
    }
  }
}

void solve() {
  bfs();
  for (int i=0; i<N; i++) {
    cout << i+1 << " " << D.at(i) << endl;
  }
}

int main() {
  input();
  solve();
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

int n;
vector<vector<int> >g;
int dist[110];
void bfs(int u) {
    queue<int> q;
    q.push(u);
    rep(i,n) dist[i] = -1;
    dist[u] = 0;
    while(!q.empty()) {
        int v = q.front();q.pop();
        for(int i = 0;i<g[v].size();i++) {
            int nv = g[v][i];
            if(dist[nv] == -1) {
                q.push(nv);
                dist[nv] = dist[v] + 1;
            }               
        }
    }
}
int main() {
    cin >> n;
    g.assign(n,vector<int>());
    rep(i,n) {
        int u,k;
        cin >> u >> k;
        u--;
        rep(j,k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    bfs(0);
    rep(i,n) cout << i+1 << " " << dist[i] << endl;
}

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

using namespace std;

struct Node{
  int p[100]={0}, d=-1, c[100], c_n;
};

Node nodes[101];

void search(int x){
    for(int i=0;i<nodes[x].c_n;++i){
        if(nodes[nodes[x].c[i]].d > nodes[x].d + 1 || nodes[nodes[x].c[i]].d == -1){
            nodes[nodes[x].c[i]].d = nodes[x].d + 1;
        }
    }
    
    for(int i=0;i<nodes[x].c_n;++i){
        if(nodes[nodes[x].c[i]].d == nodes[x].d + 1){
            search(nodes[x].c[i]);
        }
    }
    return;
}

int main(){
    int n, num, child;
    cin >> n;
    
    
    
    for(int i=1;i<=n;++i){
        cin >> num;
        
        if(num == 1){
            nodes[num].d=0;
        }
        
        cin >> nodes[num].c_n;
        for(int j=0;j<nodes[num].c_n;++j){
            cin >> nodes[num].c[j];
        }
    }
    
    search(1);
    
    for(int i=1;i<=n;++i){
        cout << i << " " << nodes[i].d << endl;
    }
    
    
    return 0;
}