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<iostream>
#include<queue>
#include<vector>
#include<algorithm>

class Graph{
private:





 class Node{
 private:
  unsigned long node_id_;
  long depth_;

  std::vector<unsigned long> adjnodes_;
 public:

  Node(const unsigned long node_id,const std::vector<unsigned long> &adjnodes)
      : node_id_(node_id),
        depth_(-1),
        adjnodes_(adjnodes){
   if(node_id_ == 1) depth_ = 0;
  };

  bool isDepth(){
   if(depth_ == -1) return true;

   return false;
  };

  void CheckNode(std::queue<unsigned long>& queue_nextnode,const std::vector<Node*>& node){
   for(unsigned long adjnode_i = 0, adjnode_max = adjnodes_.size(); adjnode_i < adjnode_max; ++adjnode_i){
    unsigned long nextadjnode = adjnodes_[adjnode_i]-1;
    if(node[nextadjnode]->isDepth()){
     node[nextadjnode]->depth_ = depth_ + 1;
     queue_nextnode.push(nextadjnode);
    }
   }
  };

  void display(){
   std::cout << node_id_ << " ";
   std::cout << depth_ << std::endl;
  };
 protected:
 };





 std::vector<Node*> node_;

public:
 Graph(unsigned long node_max) : node_(std::vector<Node*>(node_max, 0)){};
 void CreateNode(const unsigned long node_i,const unsigned long node_id,const std::vector<unsigned long>& adjnodes){
  node_[node_i] = new Node(node_id, adjnodes);
 };

 void BreadthFirstSearch(){
  if(node_.empty()) return ;

  std::queue<unsigned long> queue_nextnode;
  queue_nextnode.push(0);
  while(!queue_nextnode.empty()){
   unsigned long nextnode = queue_nextnode.front();
   queue_nextnode.pop();
   node_[nextnode]->CheckNode(queue_nextnode, node_);

  }

 };

 void display(){
  for(unsigned long node_i = 0, node_max = node_.size(); node_i < node_max; ++node_i){
   node_[node_i]->display();
  }
 };
protected:

};


int main(){

 unsigned long node_max = 0; std::cin >> node_max;

 /*
  *  ノード数分の記憶領域を確保する。
  */
 Graph graph(node_max);

 /*
  *入力はテキスト。
  *入力は一行目に    [ノード数]
  *      二行目以降  [頂点id 接続ノード数 接続ノード1 接続ノード2 ...]
  */
 for(unsigned long node_i = 0; node_i < node_max; ++node_i){
  unsigned long node_id = 0; std::cin >> node_id;
  unsigned long adjnode_max = 0; std::cin >> adjnode_max;
  std::vector<unsigned long> adjnodes(std::vector<unsigned long>(adjnode_max, 0));
  for(unsigned long adjnode_i = 0; adjnode_i < adjnode_max; ++adjnode_i){
   std::cin >> adjnodes[adjnode_i];
  }

  graph.CreateNode(node_i, node_id, adjnodes);
 }

 graph.BreadthFirstSearch();
 graph.display();

 return 0;
};

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

using namespace std;

typedef vector<int> vec;
typedef vector<vec> Graph;

Graph G(110);

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		int u,t;
		scanf("%d%d",&u,&t);
		for(int j=0;j<t;j++)
		{
			int v;
			scanf("%d",&v);
			G[u].push_back(v);
		}
	}
	queue<int> que;
	int d[110];
	fill(d+1,d+n+1,-1);
	d[1]=0;
	que.push(1);
	while(!que.empty())
	{
		int t=que.front();que.pop();
		for(int i=0;i<G[t].size();i++)
			if(d[G[t][i]]==-1||d[t]+1<d[G[t][i]])
			{
				d[G[t][i]]=d[t]+1;
				que.push(G[t][i]);
			}
	}
	for(int i=1;i<=n;i++)
		printf("%d %d\n",i,d[i]);
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int INFTY=(1<<21);
static const int N=100;
int n,M[N][N],d[N];

void bfs(int s){
  queue<int> q;
  int i,u,v;
  q.push(s);
  for(i=0;i<n;i++)d[i]=INFTY;
  d[s]=0;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(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(i=0;i<n;i++){
    cout<< i+1<<" "<<( (d[i]==INFTY)?(-1):d[i])<<endl;
  }
}

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

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

  return 0;
}

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

class Graph{
private:





 class Node{
 private:
  unsigned long node_id_;
  long depth_;

  std::vector<unsigned long> adjnodes_;
 public:

  Node(const unsigned long node_id,const std::vector<unsigned long> &adjnodes)
      : node_id_(node_id),
        depth_(-1),
        adjnodes_(adjnodes){
   if(node_id_ == 1) depth_ = 0;
  };

  bool isDepth(){
   if(depth_ == -1) return true;

   return false;
  };

  void CheckNode(std::queue<unsigned long>& queue_nextnode,const std::vector<Node*>& node){
   for(unsigned long adjnode_i = 0, adjnode_max = adjnodes_.size(); adjnode_i < adjnode_max; ++adjnode_i){
    unsigned long nextadjnode = adjnodes_[adjnode_i]-1;
    if(node[nextadjnode]->isDepth()){
     node[nextadjnode]->depth_ = depth_ + 1;
     queue_nextnode.push(nextadjnode);
    }
   }
  };

  void display(){
   std::cout << node_id_ << " ";
   std::cout << depth_ << " ";
   std::cout << std::endl;
  };
 protected:
 };





 std::vector<Node*> node_;

public:
 Graph(unsigned long node_max) : node_(std::vector<Node*>(node_max, 0)){};
 void CreateNode(const unsigned long node_i,const unsigned long node_id,const std::vector<unsigned long>& adjnodes){
  node_[node_i] = new Node(node_id, adjnodes);
 };

 void BreadthFirstSearch(){
  if(node_.empty()) return ;

  std::queue<unsigned long> queue_nextnode;
  queue_nextnode.push(0);
  while(!queue_nextnode.empty()){
   unsigned long nextnode = queue_nextnode.front();
   queue_nextnode.pop();
   node_[nextnode]->CheckNode(queue_nextnode, node_);

  }

 };

 void display(){
  for(unsigned long node_i = 0, node_max = node_.size(); node_i < node_max; ++node_i){
   node_[node_i]->display();
  }
 };
protected:

};


int main(){

 unsigned long node_max = 0; std::cin >> node_max;

 /*
  *  ノード数分の記憶領域を確保する。
  */
 Graph graph(node_max);

 /*
  *入力はテキスト。
  *入力は一行目に    [ノード数]
  *      二行目以降  [頂点id 接続ノード数 接続ノード1 接続ノード2 ...]
  */
 for(unsigned long node_i = 0; node_i < node_max; ++node_i){
  unsigned long node_id = 0; std::cin >> node_id;
  unsigned long adjnode_max = 0; std::cin >> adjnode_max;
  std::vector<unsigned long> adjnodes(std::vector<unsigned long>(adjnode_max, 0));
  for(unsigned long adjnode_i = 0; adjnode_i < adjnode_max; ++adjnode_i){
   std::cin >> adjnodes[adjnode_i];
  }

  graph.CreateNode(node_i, node_id, adjnodes);
 }

 graph.BreadthFirstSearch();
 graph.display();

 return 0;
};

C++: include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
#define int ll

int g[105][105], d[105], n;

void bfs() {
  queue<int> q;
  q.push(0);
  fill(d, d + n, -1);
  d[0] = 0; 

  while(!q.empty()) {
    int que = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (g[que][i] && d[i] == -1) {
        d[i] = d[que] + 1;
        q.push(i);
      }
    }
  }
}

signed main(void) {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      g[u-1][v-1] = 1;
    }
  }

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