Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)
#define ll long long

static const int INFTY = (1<<21);

int n, M[100][100];

void BFS(void) {
	queue<int> Q;
	int d[100];
	REP(i, n) d[i] = INFTY;
	d[0] = 0;

	Q.push(0);//?§???????0

	int u;
	while (!Q.empty()) {
		u = Q.front(); Q.pop();
		REP(i, n) {
			if (M[u][i]) {
				if (d[i] == INFTY) {
					d[i] = d[u] + 1;
					Q.push(i);
				}
			}
		}
	}

	REP(i, n) {
		cout << i + 1 << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main() {
	cin >> n;

	REP(i, n) {
		REP(j, n) M[i][j] = 0;
	}

	int u, k, v;
	REP(i, n) {
		cin >> u >> k;
		REP(j, k) {
			cin >> v;
			M[u - 1][v - 1] = 1;//0-origin
		}
	}

	BFS();

	return 0;
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int V[100][100];
int d[100];
int t[100];
int n;
queue<int> qu;
void bfs()
{
	t[0] = 1;
	d[0] = 0;
	qu.push(0);
	while (!qu.empty())
	{
		int u = qu.front(); qu.pop();
		for (int i = 0; i < n; i++)
		{
			if (V[u][i] == 1 && t[i] == 0)
			{
				t[i] = 1;
				d[i] = d[u] + 1;
				qu.push(i);
			}
		}
		t[u] = 2;
	}
	return;
}

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

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

	for (int i = 0; i < n; i++)
	{
		d[i] = -1;
		t[i] = 0;
	}
	bfs();
	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;
#define INF 1000000000

class Node{//普通の木用のデータ
//ノードはポインタでもっておこう
public:
  ///ノードの番号
  int num;
  //そのノードの親
  //Node* p;(親の情報が必要ない)
  //そのノードの子(ここ、vectorで管理してる場合はintの方が扱いやすい)
  vector<int> v;
  //探索済みかどうか
  bool visit=false;
};

void breadth_search(queue<int> vs,vector<Node>& vn,vector<int>& vd){
  int d=0;//vsにすでに格納しているものは距離が決定している前提
  int l=vs.size();
  while(l!=0){
    d++;
    l=vs.size();
    for(int i=0;i<l;i++){
      int x=vs.front();
      int l_sub=vn[x].v.size();
      for(int j=0;j<l_sub;j++){
        if(vn[vn[x].v[j]].visit==false){
          vs.push(vn[x].v[j]);
          vn[vn[x].v[j]].visit=true;
          vd[vn[x].v[j]]=d;
        }
      }
      vs.pop();
    }
  }
}


int main(){
  int n;cin >> n;
  vector<Node> vn(n);for(int i=0;i<n;i++){vn[i].num=i;}
  vector<int> vd(n,-1);//深さを保存(辿れないと-1)
  queue<int> vs;//今見ているNodeのnumを入れておく

  for(int i=0;i<n;i++){
    int k;cin >> k >> k;
    for(int j=0;j<k;j++){
      int l;cin >>l;l-=1;
      //vn[l].p=&vn[i];
      vn[i].v.push_back(l);
    }
  }

  vn[0].visit=true;
  vd[0]=0;
  vs.push(0);
  breadth_search(vs,vn,vd);
  for(int i=0;i<n;i++){
    cout << i+1 << " " << vd[i] << endl;
  }



}

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

struct Node{
	int s;
	queue<int> Q;
};

int main(){
	int n;
	cin >> n;
	Node node[n + 1];
	for(int i = 1; i <= n; i++){
		int u, k, v;
		cin >> u >> k;
		node[u].s = -1;
		while(k--){
			cin >> v;
			node[u].Q.push(v);
		}
	}

	int p = 1, next, m;
	queue<int> move;
	node[p].s = 0;
	move.push(p);

	while(!move.empty()){
		p = move.front();
		//cout << "Pivot is " << p << endl;
		if(node[p].Q.empty()){
			//cout << "Nowhere to go from " << p << endl;
			move.pop();
		} else {
			m = node[p].Q.size();
			while(m--){
				next = node[p].Q.front();
				node[p].Q.pop();
				//cout << "Checking " << next << endl;
				if(node[next].s < 0){
					//cout << "Can go to " << next << endl;
					node[next].s = node[p].s + 1;
					move.push(next);
				}
			}
			move.pop();
		}
	}

	for(int i = 1; i <= n; i++)
		cout << i << " " << node[i].s << endl;
}

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

using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

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

void bfs(int x) {
    queue<int> q;
    q.push(x);
    REP (i, n) d[i] = INFTY;

    d[x] = 0;
    while (!q.empty()) {
        int u;
        u = q.front();
        q.pop();
        REP(i, n) {
            if (M[u][i] == 0) continue;
            if (d[i] != INFTY) continue;
            d[i] = d[u] + 1;
            q.push(i);
        }
    }

    REP(i, n) {
        cout << i + 1 << " ";
        if (d[i] == INFTY) {
            cout << -1 << endl;
        } else {
            cout << d[i] << endl;
        }
    }
}

int main() {
    cin >> n;
    int v, m, k;
    REP(i, n) {
        cin >> v >> m;
        v--;
        REP(j, m) {
            cin >> k;
            k--;
            M[v][k] = 1;
        }
    }

    bfs(0);
    return 0;
}

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

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

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

coid dfs(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 =.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 <vector>
#include <queue>

int main()
{
	int n;
	std::cin >> n;
	std::vector<std::vector<int> > E(n); 
	for (int i = 0; i < n; i++)
	{
		int u, k;
		std::vector<int> G;
		std::cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++)
		{
			int g;
			std::cin >> g;
			g--;
			G.push_back(g);
		}
		E.at(u) = G;
	}
	std::vector<int> Dist(n, -1);
	Dist.at(0) = 0;
	std::queue<int> Que;
	Que.push(0);
	while (!(Que.empty()))
	{
		int s = Que.front();
		Que.pop();
		for (int i = 0; i < E.at(s).size(); i++)
		{
			int next = E.at(s).at(i);
			if (Dist.at(next) == -1)
			{
				Dist.at(next) = Dist.at(s) + 1;
				Que.push(next);
			}
		}
	}
	for (int i = 0; i < n; i++)
		std::cout << i + 1 << ' ' << Dist.at(i) << '\n';
	return 0;
}