Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1000000

int main()
{
  int n;cin>>n;
  int i,j;
  vector<vector<int>> g(n);
  rep(i,n)
  {
    int u,k;cin>>u>>k;
    u--;
    rep(j,k)
    {
      int vj;cin>>vj;
      vj--;
      g[u].emplace_back(vj);
    }
  }
  queue<int> q;
  queue<int> q2;
  q.push(0);
  vector<int> ans(n,INF);
  ans[0]=0;
  while(!q.empty())
  {
    while(!q.empty())
    {
      int now=q.front();
      q.pop();
      rep(i,g[now].size())
      {
        if(ans[g[now][i]]==INF)
        {
          q2.push(g[now][i]);
          ans[g[now][i]]=ans[now]+1;
        }
      }
    }
    while(!q2.empty())
    {
      q.push(q2.front());
      q2.pop();
    }
  }
  rep(i,n)
  {
    cout<<i+1<<' ';
    if(ans[i]==INF)cout<<-1<<endl;
    else cout<<ans[i]<<endl;
  }
}

C++: //単一始点最短経路
#include<iostream>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

void dijkstra(){
    int minv;
    int d[MAX],color[MAX];

    for(int i=0; i<n; i++){
        d[i]=INFTY;
        color[i]=WHITE;
    }

    d[0]=0;
    color[0]=GRAY;

    while(1){
        minv=INFTY;
        int u=-1;
        for(int i=0; i<n; i++){
            if(minv>d[i] && color[i]!=BLACK){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1){ break; }
        color[u]=BLACK;
        for(int v=0; v<n; v++){
            if(color[v]!=BLACK &&  M[u][v]!=INFTY){
                if(d[v]>d[u]+M[u][v]){
                    d[v]=d[u]+M[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }
    for(int i=0; i<n; i++){
        cout<<i<<" "<<(d[i]==INFTY ? -1 : d[i])<<endl;
    }
}

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

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

    dijkstra();
    return 0;
}

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

using namespace std;

void bfs(int v,vector<vector<int>>& vertex, int d[]);

//int depth = 0;

int main(){
    int n;
    cin >> n;
    int num;
    int tmp2;
    int d[n];
    for(int i=0;i<n;i++){
        d[i] = -1;
    }
    vector<vector<int>> vertex;
    vector<int> tmp;
    for(int i=0;i<n;i++){
        cin >> num;
        cin >> num;
        for(int j=0;j<num;j++){
            cin >> tmp2;
            tmp.push_back(tmp2-1);
        }
        vertex.push_back(tmp);
        tmp.clear();
    }
    bfs(0, vertex, d);
    
    for(int i=0;i<n;i++){
        cout << i+1 << " " <<  d[i] << endl;
    }
    return 0;
}

void bfs(int v, vector<vector<int>>& vertex, int d[]){
    queue<int> Q;
    Q.push(v);
    d[v] = 0;
    while(!Q.empty()){
        v = Q.front();
        Q.pop();
        bool flag = false;
        for(int i : vertex[v]){
            if(d[i]==-1){
                d[i] = d[v] + 1;
                Q.push(i);
                flag = true;
            }
        }
    }
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if(graph[u][i] == 0 continue;
      if(d[i] != no) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}

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

struct{
	int link;
	int stg;
}typedef LinkStage;

class Vertex{
	private:
		int* links;
		int index;
		int num;
		bool arrived;
		int id;
		int d;
	public:
		Vertex(int u,int k):id(u), num(k),links(new int[k]),index(0),arrived(false),d(-1){}
		void addLink(int);
		void enQueueLinks(queue<LinkStage>*,int);	
		void print();
};

void Vertex::addLink(int link){
	links[index] = link - 1;
	index++;
}

void Vertex::enQueueLinks(queue<LinkStage>* taskQueue,int dist){
	if(arrived)
		return;

	for(int i = 0; i < index;i++){
		LinkStage stage;
		stage.link = links[i];
		stage.stg = dist + 1;
		taskQueue->push(stage);
	}
	arrived = true;
	d = dist;
}

void Vertex::print(){
	cout << id << " " << d << endl;
}

int main(){
	int n;
	cin >> n;
	Vertex** vertexes = new Vertex*[n];
	for(int i = 0;i < n;i++){
		int u;
		cin >> u;
		int k;
		cin >> k;
		Vertex* vertex = new Vertex(u,k);
		for(int j = 0;j < k;j++){
			int v;
			cin >> v;
			vertex->addLink(v);
		}
		vertexes[u - 1] = vertex;
	}
	queue<LinkStage> task;
	vertexes[0]->enQueueLinks(&task,0);
	while(!task.empty()){
		LinkStage curr = task.front();
		task.pop();
		int nextI = curr.link;
		int d = curr.stg;
		vertexes[nextI]->enQueueLinks(&task,d);
	}
	for(int i = 0; i < n;i++){
		vertexes[i]->print();
	
	}
	
	return 0;
}

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

int n;
vector<vector<int> > G;
vector<int> D;

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    D[src] = 0; // ?????????
    while(!Q.empty()){
        int cur = Q.front(); // ??????????´???????????????????
        Q.pop();
        for(int dst = 0; dst < n; ++dst){ // ???????????? dst ????????????
            if(G[cur][dst] == 1 && D[dst] == -1){ // cur??????dst???????????????,dst???????¨??????????
                D[dst] = D[cur] + 1;
                Q.push(dst); // dst????¨??????????????????????
            }
        }
    }
}

int main(){
    cin >> n;
    G = vector<vector<int> >(n, vector<int>(n, 0));
    D = vector<int>(n, -1);
    for(int i=0; i<n; ++i){
        int u;
        cin >> u;
        // 1~n???index?????¨????????????????????????????????????1????????????0~n-1????????????
        u -= 1;
        int k;
        cin >> k;
        for(int j=0; j<k; ++j){
            int v;
            cin >> v;
            v -= 1;
            G[u][v] = 1;
        }
    }
    bfs(0);
    for(int i=0; i<n; ++i){
        cout << i+1 << " " << D[i] << endl;
    }
}