Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

static const int N = 100;
static const int INF = (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] = INF;
	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] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-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--; // 0オリジン
		for (int j = 0; j < k; j++) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0);
	
	return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int main() {
    int V, k, source, target, current, next;
    bool oneVisited;
    scanf("%d", &V);
    vector<int> G[V+1];
    bool visited[V+1];
    int disc[V+1];
    fill(visited, visited+V+1, 0);
    fill(disc, disc+V+1, -1);
    for (int i = 0; i < V; i++) {
        scanf("%d %d", &source, &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &target);
            G[source].push_back(target);
        }
    }
    queue<int> q;
    q.push(1);
    int d = 0; disc[1] = 0;
    visited[1] = true;
    while (!q.empty()) {
        current = q.front(); q.pop();
        for (int i = 0; i < G[current].size(); i++) {
            next = G[current][i];
            if (!visited[next]) {
                visited[next] = true;
                disc[next] = disc[current]+1;
                q.push(next);
            }
        }
    }
    for (int i = 1; i <= V; i++) printf("%d %d\n", i, disc[i]);
    return 0;
}

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

#define M 1000000007
vector<int> dist;
vector<vector<int>> edge;

int main() {
    int n;
    cin >> n;
    dist.resize(n, M);
    edge.resize(n);
    for (int i = 0; i < n; ++i) {
        int id, k;
        cin >> id >> k;
        if (id != i + 1) exit(1);
        if (!k) continue;

        edge[i].resize(k);
        for (int j = 0; j < k; ++j) {
            cin >> edge[i][j];
            --edge[i][j];
        }
    }
    queue<int> q;
    q.push(0);
    dist[0] = 0;
    while (!q.empty()) {
        int p = q.front();
        q.pop();
        for (int t : edge[p]) {
            if (dist[t] > dist[p] + 1) {
                dist[t] = dist[p] + 1;
                q.push(t);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << (i + 1) << " " << (dist[i] < M ? dist[i] : -1) << "\n";
    }
    return 0;
}

C++: #include<iostream>
#include<queue>
#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();
  queue<int> que;
  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] << " " << ans[u[i]] << endl;
  }

  return 0;
}

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

#define NODE_NUM 100

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int id);
	int dist;
	int check[NODE_NUM];
};

Node::Node()
{
	next_num = 0;
	dist = -1;
	for (int i = 1; i <= NODE_NUM; i++) {
		check[i] = 0;
	}
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node Node_list[NODE_NUM + 1];

bool Node::Search(int id)
{
	check[id] = 1;

	for (int k = 0; k < next_num; k++) {
		if (Node_list[next_node_list[k]].dist == -1) {
			Node_list[next_node_list[k]].dist = dist + 1;
		}
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == id) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		if (Node_list[next_node_list[j]].check[id] == 0) {
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u;
		cin >> k;
		Node_list[u] = Node();
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			Node_list[u].AddNextNode(v);
		}
	}
	
	//dist
	Node_list[1].dist = 0;
	for (int i = 2; i <= n; i++) {
		bool ret = Node_list[1].Search(i);
		if (ret) continue;
		if (que.empty()) continue;
		do {
			ret = Node_list[que.front()].Search(i);
			que.pop();
		} while (!ret);
		while (!que.empty()) {
			que.pop();
		}
	}

	//output
	for (int i = 1; i <= n; i++) {
		cout << i << " " << Node_list[i].dist << "\n";
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void){
	int n;
	int u,k,v;
	int box[101][101];
	int d[101];
	int i,j;
	int x,y;
	queue<int>que;
	
	cin>>n;
	for(i=1;i<=n;i++){
		d[i]=0;
		for(j=1;j<=n;j++){
			box[i][j]=0;
		}
	}
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			box[u][v]=1;
		}
	}
	d[1]=0;
	que.push(1);
	while(!que.empty()){
		x=que.front();
		que.pop();
		for(i=1;i<=n;i++){
			if(box[x][i]==1){
				y=i;
				if(d[y]==0){
					d[y]=d[x]+1;
					que.push(y);
				}
			}
		}
	}
	cout<<1<<" "<<d[i]<<endl;
	for(i=2;i<=n;i++){
		if(d[i]==0)	d[i]=-1;
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}