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) {
			if !dl.isVisited(v) {
				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 <climits>

using namespace std;

enum COLOR {
  WHITE,
  GRAY,
  BLACK,
};

static const int INFTY = INT_MAX;
static const int N = 100;

static int M[N][N] = {};
static vector<COLOR> color(N, WHITE);
static vector<int> d(N, INFTY);

static void bfs(int n, int s) {
  color[s] = GRAY;
  d[s] = 0;

  queue<int> Q;
  Q.push(s);

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] && color[v] == WHITE) {
          color[v] = GRAY;
          d[v] = d[u] + 1;
          Q.push(v);
      }
      color[u] = BLACK;
    }
  }
}

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

  int number, count, value;
  for (int i = 0; i < n; i++) {
    cin >> number >> count;
    number--;
    for (int j = 0; j < count; j++) {
     cin >> value;
     value--;
     M[number][value] = 1; 
    }
  }
  
  bfs(n, 0);
  
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? -1 : d[i]) << endl; 
  }

  return 0;
}

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

int main(){
  vector<int> G[100];
  queue<int> q;
  vector<int> d(100,-1);
  int n,u,k,v,depth;
  bool flg;
  
  cin >> n;
  for (int i=0;i<n;i++){
    cin >> u>> k;
    for (int j=0;j<k;j++){
      cin >> v;
      G[u-1].push_back(v-1);
    }
  }
  depth=0;
  q.push(0);
  d[0]=depth;
  while (!q.empty()){
    u=q.front();
    for (int i=0;i<G[u].size();i++){
      v=G[u][i];
      if (d[v]==-1){
	q.push(v);
	d[v]=d[u]+1;
	flg=true;
	break;
      }
    }
    if (!flg){
      q.pop();
    }else{
      flg=false;
    }
  }
    
  for (int i=0;i<n;i++)
    cout << i+1 <<" "<< d[i]<<endl;
  
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <limits.h>
using namespace std;
vector<int> v[101];
int d[101];
queue<int> q;
bool flag[101];
int main(){
	int n, u, k; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; ++j){
			int tmp; scanf("%d", &tmp);
			v[u].push_back(tmp);
		}
	}
	memset(d, -1, sizeof(d));
	d[1] = 0;
	q.push(1); flag[1] = true;
	while (!q.empty()){
		int m = q.front(); q.pop();
		for (int i = 0; i < v[m].size(); ++i){
			int tmp = v[m][i];
			if (!flag[tmp]){
				flag[tmp] = true;
				d[tmp]=d[m]+1;
				q.push(tmp);
			}
		}
	}
	for (int i = 1; i <= n; ++i){
		cout << i << ' ' << d[i] << '\n';
	}
}

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

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n; cin >> n;
  vector<vector<int> > E(n, vector<int>());
  REP(i, n){
    int u, k; cin >> u >> k; u--;
    REP(j, k){
      int v; cin >> v; v--;
      E[u].push_back(v);
    }
  }
  vector<int> d(n, -1);
  vector<bool> dd(n, false);
  d[0] = 0;
  dd[0] = true;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    int cur = q.front(); q.pop();
    for(auto &i : E[cur]){
      if(dd[i]) continue;
      d[i] = d[cur] + 1;
      q.push(i);
      dd[i] = true;
    }
  }
  REP(i, n){
    cout << i+1 << " " << d[i] << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
 
class Queue{
private:
  int inf[MAX];
  int head,tail;
  
public:
  
  Queue(){
    syokika();
  }
  
  void syokika(){
    head = 0;
    tail = 0;
  }
  
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
  
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
  
  int front(){
    return inf[head];
  }
  
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
  
  int size(){
    return tail - head;
  }
  
};
 
int imap[200][200];
int k[200] = {0};
int ans[200] = {0};
bool used[200];
 
int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF; 
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }
 
   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j;
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }
 
  Queue que = Queue();
 
  que.push(u[0]);
 
  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
    que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
    used[imap[p][i]] = true;
    ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
  
  for(int i=0;i<n;i++){
    cout << u[i]-1 << " " << ans[u[i]] << endl;
  }
 
  return 0;
}

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

//breadth first search

int main() {
	int n, u, k, v;
	vector< vector< int > > list;
	vector< int > dist;
	queue< int > que;
	
	cin >> n;
	list.resize(n);
	dist.resize(n);
	
	for(int i=0;i<n;++i){
		cin >> u >> k;
		dist[i] = -1;
		
		for(int j=0;j<k;++j){
			cin >> v;
			list[u-1].push_back(v-1);
		}
	}
	
	que.push(0);
	dist[0] = 0;
	
	while(!que.empty()){
		int cur = que.front();
		vector< int > branch = list[cur];
		que.pop();
		
		for(vector< int >::iterator it=branch.begin();it<branch.end();++it){
			if(dist[*it] == -1){
				que.push(*it);
				dist[*it] = dist[cur] + 1;
			}
		}
	}
	
	for(int i=0;i<n;++i){
		cout << i+1 << " " << dist[i] << endl;
	}
	
	return 0;
}