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 <algorithm>
#include <queue>
#define N 100
using namespace std;

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


void bfs(int n) {
	queue<int> Q;
	d[0] = 0;
	for (int i = 1;i < n;i++) d[i] = -1;
	Q.push(0);
	color[0] = 1;
	while (!Q.empty()) {
	  for (int i = 0;i < n;i++)
		  if (M[Q.front()][i] == 1) {
			  if (color[i] == 0) {
				  d[i] = d[Q.front()] + 1;
				  color[i] = 1;
				  Q.push(i);
			  }
		  }
		  Q.pop();
	}
	for (int i = 0;i < n;i++)
		cout << i + 1 << " " << d[i] << endl;
}


int main() {
	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 v; cin >> v;
			M[u-1][v-1] = 1;
		}
	}

	bfs(n);

}

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
#define MAX 100
#define INFTY -1000
typedef long long ll;
int n;
vector<int>A[MAX];
int d[MAX];
int color[MAX]={0};
void bfs(){
  	rp(i,n)d[i]=INFTY;
    queue<int>Q;
    Q.push(0);
    color[0]=1;
  	d[0]=0;
    while(!Q.empty()){
        int x=Q.front();
        Q.pop();
        for(auto i:A[x]){
            if(color[i]==0){
                Q.push(i);
                d[i]=d[x]+1;
              	color[i]=1;
            }
        }
    }
    rp(i,n){
      	if(d[i]==INFTY)cout<<i+1<<" "<<-1<<endl;
      	else cout<<i+1<<" "<<d[i]<<endl;
    }
}
int main(){
   int u,k,v;
   cin>>n;
   rp(i,n){
       cin>>u;u--;
       cin>>k;
       rp(j,k){
           cin>>v;v--;
           A[u].push_back(v);
       }
   }
   bfs();
}

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

int main() {
	
	int n;
	cin >> n;
	vector<vector<int>> e(n,vector<int>());
	vector<int>dist(n,-1);
	queue<int> que;
	for(int i=0; i<n; i++){
		int u,k;
		cin >> u >> k;
		for(int j=0; j<k; j++){
			int a;
			cin >> a;
			a--;
			e.at(i).push_back(a);
		}
	}
	que.push(0);
	dist.at(0) = 0;
	while(!que.empty()){
		int v = que.front();
		que.pop();
		for(auto nv:e.at(v)){
			if(dist.at(nv)!=-1) continue;
			dist.at(nv) = dist.at(v)+1;
			que.push(nv);
		}
	}
	for(int i=0; i<n; i++){
		cout << i+1 << ' ' << dist.at(i) << endl;
	}
	
	
}

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

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main()
{
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<vector<int> > mat(n + 5, vector<int>(n + 5, 0));
  for (int i = 0; i < n; ++i) {
    int v = -1, k = -1, u = -1;
    fscanf(stdin, "%d %d", &v, &k);
    for (int j = 0; j < k; ++j) {
      fscanf(stdin, "%d", &u);
      mat[v][u] = 1;
    }
  }
  vector<int> cost(n + 5, -1);
  queue<int> que;
  cost[1] = 0;
  que.push(1);
  while (!que.empty()) {
    int v = que.front(); que.pop();
    for (int u = 1; u <= n; ++u) {
      if (mat[v][u] && cost[u] == -1) {
        cost[u] = cost[v] + 1;
        que.push(u);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    fprintf(stdout, "%d %d\n", i, cost[i]);
  }
  return 0;
}

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

template<typename T>
T gcd(T a, T b)
{
	return b ? gcd(b, a%b) : a;
}

template<typename T>
T lcm(T a, T b)
{
	return a / gcd(a, b) * b;
}

//(a^e)%mod
template<typename T>
T power(T a, T e, T mod)
{
	if(e == 0)
	{
		return 1;
	}
	if(e % 2 != 0)
	{
		//a^e=a^(e-1)*a
		return (power(a, e - 1, mod) * a) % mod;
	}
	T res = power(a, e / 2, mod);
	//a^e=a^(e/2)*a^(e/2)
	return (res * res) % mod;
}

template<typename T>
class Tree
{
private:
public:
	T data;
	Tree* parent = NULL;
	std::vector<Tree*> children;
	void setData(T x)
	{
		data = x;
	}
	void setParent(Tree* t)
	{
		parent = t;
	}
	void addChild(Tree* t)
	{
		children.push_back(t);
	}
	Tree(){}
	~Tree(){}
	
};

template<typename T>
class Graph
{
public:
	T index;
	vector<T> edge;
	void setEdge(T t)
	{
		edge.push_back(t);
	}
	Graph(){}
	~Graph(){}
};

int main()
{
	using namespace std;
	int n;
	cin >> n;
	Graph<int> G[101];
	for (int i = 0; i < n; ++i)
	{
		int index, k;
		cin >> index >> k;
		G[index].index = index;
		for (int j = 0; j < k; ++j)
		{
			int in;
			cin >> in;
			G[index].setEdge(in);
		}
		sort(G[index].edge.begin(), G[index].edge.end());
	}
	int kyori[101] = {};
	queue<pair<int,int>> Q;
	Q.push(make_pair(1, 0));
	bool visited[101] = {};
	while(!Q.empty())
	{
		pair<int,int> now = Q.front();
		Q.pop();
		if(visited[now.first])
		{
			continue;
		}
		visited[now.first] = 1;
		kyori[now.first] = now.second;
		for (auto a : G[now.first].edge)
		{
			Q.push(make_pair(a, now.second + 1));
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		cout << i << " " << (visited[i]==0?-1:kyori[i]) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100;
int n;
int d[N+1];
vector< vector<int> > ns;
queue<int> q;

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  vector<int> v;
  ns.push_back(v);
  cin >> n;
  FOR(i, 1, n+1) {
    int id, k, x; cin >> id;
    vector<int> v;
    cin >> k;
    REP(j, k) {
      cin >> x;
      v.push_back(x);
    }
    ns.push_back(v);
  }

  FOR(i, 1, n+1) d[i] = -1;

  int u = 1;
  q.push(u);
  d[u] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    REP(i, ns[u].size()) {
      int v = ns[u][i];
      if (d[v] == -1) {
        q.push(v);
        d[v] = d[u] + 1;
      }
    }
  }

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