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 <iostream>
#include <vector>
#include <queue>
using namespace std;

int n;
vector <int> G[101];
queue <int> Q;
int  d[100];
int  dis[101];


int visit(int x){
  int t,i;
  
  Q.push(x);
  dis[x] = 0;
  d[x] = 1;

  while(Q.empty() == false) {
    
    cout << "start Q" <<endl;

    t = Q.front();
    Q.pop();

    for(i=0; i<G[t].size();i++) {
      if(d[G[t][i]] == 0 ) {
	d[G[t][i]] = 1;
	dis[G[t][i]] = dis[t] + 1;

	Q.push(G[t][i]);
      }

    }
  }
}
    
int main() {
  
  int i,j,k,l,u,a,t;
  
  
  scanf("%d",&n);

  
  
  for(i=0;i<n;i++){
    d[i] = 0;
    dis[i] = -1;
    scanf("%d %d ",&k,&u);
    
    for(j=0;j<u;j++) {
      scanf("%d",&a);
      G[k-1].push_back(a-1);
      // printf("G[%d][%d] = 1\n",i,k-1);
    }
  }
  
  
  visit(0);

  

  for(i=0;i<n;i++) cout << i+1<<" " << dis[i] <<endl;



  return 0;

    
    
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int MAX_N = 150;
template<class Type> class Queue
{
    private:
        struct Node { Type num; Node *back; };
        Node *head, *tail;
        int sz;
    public:
        Queue() { head = tail = new Node; sz = 0; }
        void push(Type x)
        {
            Node *n = new Node;
            n->num = x;
            tail->back = n; tail = n;
            if(empty()) head = tail; sz++;
        }
        Type pop()
        {
            Type res = head->num;
            Node *_head = head;
            head = head->back;
            delete _head; sz--;
            return res;
        }
        Type front() { if(not empty()) return head->num; }
        bool empty() { return sz == 0; }
};
int Gragh[MAX_N][MAX_N];
int used[MAX_N];
void bfs(int u, int n)
{
    used[u] = 0;
    Queue<int> que;
    que.push(u);
    while(!que.empty())
    {
        int u = que.front(); que.pop();
        for(int v = 1; v <= n; v++)
        {
            if(Gragh[u][v] == -1 || used[v] != -1) continue;
            used[v] = used[u] + 1;
            que.push(v);
        } 
    }
}
void init(int n)
{
    for(int i = 0; i <= n; i++)
    {
        used[i] = -1;
        for(int j = 0; j <= n; j++) Gragh[i][j] = -1;
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    init(n);
    for(int i = 0; i < n; i++)
    {
        int u, k; cin >> u >> k;
        for(int j = 0; j < k; j++)
        {
            int v; cin >> v;
            Gragh[u][v] = 1;
        }
    }
    bfs(1, n);
    for(int u = 1; u <= n; u++) cout << u << " " << used[u] << endl;
    return 0;
}

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

struct node
{
  int name;
  std::vector<node*> neighbors;
  int distance{-1};
};

void solve(std::vector<node>& nodes)
{
  std::queue<node*> queue;
  auto& root = nodes.front();
  root.distance = 0;
  queue.push(&root);

  while (!queue.empty()) {
    auto* target = queue.front();
    queue.pop();

    for (auto* n : target->neighbors) {
      if (n->distance >= 0) continue;
      n->distance = target->distance + 1;
      queue.push(n);
    }
  }
}

int main()
{
  int n;
  std::cin >> n;

  std::vector<node> nodes(n);
  for (int i = 0; i < n; ++i) {
    int u, k;
    std::cin >> u >> k;
    auto& node = nodes.at(u - 1);
    node.name = u;
    node.neighbors.resize(k);
    for (auto& nb : node.neighbors) {
      int v;
      std::cin >> v;
      nb = &nodes.at(v - 1);
    }
  }

  solve(nodes);

  for (const auto& node : nodes) {
    std::cout << node.name << ' ' << node.distance << std::endl;
  }
}

C++: #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int,int> PII;
typedef long long LL;

#define d(x)  cout<<#x<<" = "<<(x)<<endl;
#define c cout<<"#"<<endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define DREP(i,n) DFOR(i,n,0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);
////////////////////////////////////////////////
int n,a[101][101],d[101],p[101];
QI q;
int main()
{
	cin>>n;
	FOR(i,1,n+1)
	{
		int k,m;cin>>k>>m;
		a[k][0]=m;
		FOR(j,1,m+1)
		{	
			int x;cin>>x;
			a[k][j]=x;
		}
	}
	REP(i,101)p[i]=1;
	for(q.push(1);SZ(q)!=0;)
	{
		int u=q.front();
		q.pop();
		for(;p[u]<=a[u][0];)
		{
			int v=a[u][p[u]];p[u]++;
			if(v!=1&&d[v]==0)
			{
				d[v]=d[u]+1;
				q.push(v);
			}
		}
	}
	FOR(i,2,n+1)
	{
		if(d[i]==0)d[i]--;
	}
	FOR(i,1,n+1)
	{
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}