Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define arrdump(n,a) for(int i=0;i<n;i++) {cout<<((i>0)? " ":"")<<a[i];}cout<<"\n";


typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja

int d[105];
vector<vector<int>> vv;

int main() {
    int n; cin >> n;
    REP(i, n) d[i] = -1;
    vv.resize(n);
    REP(i, n) {
        int u, k; cin >> u >> k;
        u--;
        REP(j, k) {
            int v; cin >> v;
            v--;
            vv[u].push_back(v);
        }
    }

    queue<int> q;
    q.push(0);
    d[0] = 0;

    while (!q.empty()) {
        int f = q.front(); q.pop();

        for (int t : vv[f]) {
            if (d[t] == -1 || d[t] > d[f] + 1) {
                d[t] = d[f] + 1;
                q.push(t);
            }
        }
    }

    REP(i, n) {
        cout << i+1 << " "<< d[i] << "\n";
    }
       

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
//original idea: stupid wanna check the node with many bfs search 
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1
queue<int> q;
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
int vis[N];
int res = 0;
void bfs(int d){
	if(q.empty()) return;
	//until q is empty
	while(!q.empty()){
		int temp = q.front();
		q.pop();
		if(dist[temp]!=-1){
			if(d<dist[temp]) dist[temp] = d;
			else {dist[temp] = dist[temp];}
		}else {
			dist[temp] = d;
		}
		for(int i = 0; i<neighbors[temp].size(); i++){
			d = dist[temp] +1;//next lever
		if(dist[neighbors[temp].at(i)] == -1){//not visited
			q.push(neighbors[temp].at(i));
			dist[neighbors[temp].at(i)] = d;
			}
		}
		
	}
	
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, -1, sizeof(dist));
    memset(vis, -1, sizeof(dist));
    for(int i = 0; i<n; i++){
        int node;
        scanf("%d",&node);
        int num;
        scanf("%d",&num);
        for(int j = 0; j<num; j++){
        	int temp;
        	scanf("%d",&temp);
        	neighbors[node].push_back(temp);
        	//neighbors[temp].push_back(node);
		}
    }
	q.push(1);//begin from 1
    bfs(0);
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

vector <int> adj[105];
int level[105];
int visited[105];

void bfs()
{
    for(int i = 0; i < 105; i++)
        level[i] = -1;

    queue <int> q;
    q.push(1);
    visited[1] = 1;
    level[1] = 0;

    while(!q.empty())
    {
        int u = q.front();
        q.pop();

        for(int i = 0; i < adj[u].size(); i++)
        {
            int v = adj[u][i];

            if(!visited[v]) {
                visited[v] = 1;
                level[v] = level[u] + 1;
                q.push(v);
            }
        }
    }
}

int main()
{
    int node, vertice, edge, vertex, u, v, i, j;

    cin >> node;

    for(i = 0; i < node; i++)
    {
        cin >> vertice >> edge;
        for(j = 0; j < edge; j++) {
            cin >> vertex;
            adj[vertice].push_back(vertex);
        }
    }

    bfs();

    for(i = 1; i <= node; i++) {
        cout << i << " " << level[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

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

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

int main(){
  int n;
  cin>>n;
  int a[110][110] = {};
  for(int i=1;i<=n;i++){
    int u,k;
    cin>>u>>k;
    for(int j=0;j<k;j++){
      int v;
      cin>>v;
      a[i][v]=1;
    }
  }

  int d[110];
  for (int i=1;i<=n;i++){
    d[i]=-1;
  }
  queue<int> q;
  d[1]=0;
  q.push(1);
  while(! q.empty()){
    int cur =q.front();
    q.pop();
    for(int i=1;i<=n;i++){
      if(a[cur][i]==1 && d[i]==-1){
	d[i]=d[cur]+1;
	q.push(i);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" "<<d[i]<<endl;
  }
}