Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

static const int N = 100;
static const int INFTY = (1<<21);
int n, M[N][N];
int d[N];

void bfs(int s) {
	queue<int> q;
	q.push(s);
	for ( int i = 0; i < n; i++ ) d[i] = INFTY;
	d[s] = 0;
	int u;
	while ( !q.empty() ) {
		u = q.front(); q.pop();
		for ( int v = 0; v < n; v++ ) {
			if ( M[u][v] == 0 ) continue;
			if ( d[v] != INFTY ) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for (int i = 0; i<n; i++ ) {
		cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
	}
}

int main() {
	int u, k, v;

	cin >> n;
	for (int i = 0; i < n; i++ ) {
		for (int j = 0; j < n; j++ ) M[i][j] = 0;
	}

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

	bfs(0);

	return 0;
}

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

int main()
{

	int n;
	cin >> n;
	vector<vector<int>> g(n+1);

	for (int i=1;i<=n;++i)
	{
		int u,k;
		cin >> u >> k;
		for (int j=0;j<k;++j)
		{
			int v;
			cin >> v;
			g[u].push_back(v);
		}
	}

	vector<int> dist(n+1);
	for (int i=1;i<=n;++i)
	{
		dist[i]=-1;
	}
	queue<int> q;

	q.push(1);
	dist[1]=0;
	while (q.empty()==false)
	{
		int node=q.front();
		q.pop();

		for (int i=0;i<g[node].size();++i)
		{
			int nn=g[node][i];
			if (dist[nn]<0)
			{
				dist[nn]=dist[node]+1;
				q.push(nn);
			}
		}
	}

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

C++: #include <stdio.h>
#include <stdlib.h>

struct node_t {
    int key;
    struct node_t *next;
};
typedef struct node_t Node;

Node *enqueue(Node *node, int key) {
  Node *p, *q;

  if (node == NULL) {
    node = malloc(sizeof(Node));
    node->key = key;
    node->next = NULL;
    return node;
  }

  for (p = node; p != NULL; p = p->next) {
    q = p;
  }
  q->next = malloc(sizeof(Node));
  q->next->key = key;
  q->next->next = NULL;
  return node;
}

Node *dequeue(Node *node, int *key) {
  Node *p;

  if (node != NULL) {
    *key = node->key;
    p = node->next;
    free(node);
        return p;
  }
  return NULL;
}

int main(void) {
  int num, i, j, k, u;
  int way[101][101] = {{0}};
  int d[101];
  Node *q = NULL;

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d", &way[u-1][j]);
    }
  }

  d[0] = 0;
  for (i = 1; i < num; i++) {
    d[i] = -1;
  }
  q = enqueue(q, 0);

  while (q != NULL) {
    q = dequeue(q, &u);
    for (i = 0; i < num; i++) {
      k = way[u][i] - 1;
      if (d[k] < 0) {
    d[k] = d[u] + 1;
    q = enqueue(q, k);
      }
    }
  }

  for (i = 0; i < num; i++) {
    printf("%d %d\n", i+1, d[i]);
  }
  return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
intd  d[N];

void bfs(int s)  {
  queue<int> q;
  q.push(s);
  for (int i = 0; i< n; i++)  {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty())  {
    u = q.front(); q.pop();
    for (int v = 0; v < n; v++)  {
      if (M[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++)  {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

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

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

using namespace std;

const int N = 100;


int main() {
    int A[N][N] = {{0}};

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int u;
        cin >> u;

        int k;
        cin >> k;
        for (int j = 0; j < k; ++j) {
            int x;
            cin >> x;
            A[u-1][x-1] = 1;
        }
    }

    vector<int> d(n, -1);
    using int_pair = pair<int, int>;
    queue<int_pair> Q;
    Q.push(int_pair(0, 0));
    while (!Q.empty()) {
        auto p = Q.front();
        Q.pop();

        int k = p.first;
        int t = p.second;
        if (d[k] > t)
            continue;

        if (d[k] == -1) {
            d[k] = t;
        } else {
            d[k] = min(d[k], t);
        }
        for (int i = 0; i < n; ++i) {
            if (A[k][i] != 0)
                Q.push(int_pair(i, t + 1));
        }
    }

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

    return 0;
}

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

int main() {
    // 頂点数と辺数
    int N ; cin >>N;

    // グラフ入力受取
    Graph G(N);
    for (int i = 0; i < N ;i++) {
        int a, b; cin >> a >> b;
        for(int j=0;j<b;j++) {
          int c; cin >>c;
          G[a-1].push_back(c-1);
        }
    }
     // BFS のためのデータ構造
    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[0] = 0;
    que.push(0); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 0; v < N; ++v) cout << v+1 << " " << dist[v] << endl;
}

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

using namespace std;

int N;
int D[100+10] = {};
int U = 0;
int K[100+10] = {0};
int V = -1;
int G[100+10][100+10] = {0};



void bfs(int src){
  //cerr << "bfs root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (! Q.empty()){
    int cur = Q.front();
    Q.pop();
    //cerr << "visiting " << cur << " " << D[cur] << endl;
    for(int dst=1;dst<=N;++dst){
      if(D[dst] == -1 && G[cur][dst] == 1){
	D[dst] = D[cur]+1;
	Q.push(dst);
      }
    }
  }
}

int main(){
  cin >> N;
  for(int i=0;i<101;++i){
    D[i] = -1;//?????????
  }

  for(int i=0;i<N;++i){
    cin >> U;
    cin >> K[U];
    for(int j=0;j<K[U];++j){
      cin >> V;
      G[U][V] = 1;
    }
  }
  /*
  for(int j=1;j<=N;++j){
    for(int i=1;i<=N;++i){
      cout << G[i][j] << " ";
    }
    cout << endl;
   }
*/

  bfs(1);
  
  for(int i=1;i<=N;++i){
    cout << i << " " << D[i] << endl;
  }

}