Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 10000
#define NIL -1
static const int N = 100;
static const int INFTY = 10000000;

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);

}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n;
  cin>>n;
  vector<vector<int> > G(n,vector<int>());
  for(int i=0;i<n;i++){
    int u,k;
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      int c;
      cin>>c;
      c--;
      G[u].push_back(c);
    }
  }
  int d[n];
  memset(d,-1,sizeof(d));
  queue<int> q;
  d[0]=0;
  q.push(0);
  while(!q.empty()){
    int v=q.front();q.pop();
    for(int u:G[v]){
      if(d[u]<0){
	d[u]=d[v]+1;	
	q.push(u);
      }
    }
  }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<d[i]<<endl;;
  return 0;
}

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

using namespace std;
typedef long long int ll;

int dh[4] = {1,0,-1,0};
int dw[4] = {0,1,0,-1};

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

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N=100;
static const int INFTY=2000000000;
int n,M[N+1][N+1];
int d[N+1];
void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=1;i<=n;i++)d[i]=INFTY;
    d[s]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int v=1;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=1;i<=n;i++)
        cout<<i<<" "<<((d[i]==INFTY)?-1:d[i])<<"\n";

}

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

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

vector<int> bfs(
    int p,
    const vector<vector<int>> graph)
{
    queue<int> Q;
    vector<bool> status(graph.size());
    vector<int> distance(graph.size());
    Q.push(p);
    status[p] = true;
    while (!Q.empty())
    {  
        int k = Q.front();
        Q.pop();
        for (const auto& q : graph[k]) {
            if (!status[q]) {
                Q.push(q);
                status[q] = true;
                distance[q] = distance[k]+1;
            }
        }
    }
    for (int i = 0; i < status.size(); ++i) {
        if (status[i] == 0) {
            distance[i] = -1;
        }
    }
    return distance;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n);
    for (auto&& g : graph) {
        int id;
        cin >> id;
        int size;
        cin >> size;
        g.reserve(size);
        for (int j = 0; j < size; ++j) {
            int k; 
            cin >> k;
            g.push_back(k-1);
        }
    }
    auto result = bfs(0, graph);
    for (int i = 0; i < result.size(); ++i) {
        cout << i+1 << " " << result[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------


int main() {
   int n;
   cin>>n;
   vi ans(n,-1);
   vi g[n];
   rep(i,n){
       int h,k;
       cin>>h>>k;
       rep(j,k){
           int t;
           cin>>t;
           //cout<<i<<" "<<t-1<<endl;
           g[i].push_back(t-1);
       }
   }
  
   ans.at(0)=0;
   queue<int> q ;
   q.push(0);
   
   while(!q.empty()){
      int from=q.front();
      q.pop();
      for(int to : g[from]){
          if(ans.at(to)!=-1)continue;
          ans.at(to)=ans.at(from)+1;
          q.push(to);
      }
   }
   rep(i,n){
       cout<<i+1<<" "<<ans.at(i)<<endl;
   }
}

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

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nFromMeTo;

  StDist(int nDist) : m_nDist(nDist), m_nFromMeTo(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    if (rvoDist[nx].m_nDist == nIMPOS)  continue;
    if (rvoDist[nx].m_nFromMeTo == 1 )  continue; 

    rvoDist[nx].m_nFromMeTo = 1;
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
      if (cnrvvnAdjcy[nx][ny] == 1)
      {
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;
        else
          rvoDist[ny].m_nDist = min(rvoDist[ny].m_nDist, nDist);

        rqnBFSrch.push(ny);
      }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}