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 <queue>
#define MAX 101
#define NIL -1

using namespace std;


int matrix[MAX][MAX] = {0};
int dis[MAX];
//提交时须对time改名 
int n;


void bfs_visit(int u);
int next(int u);

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> v;
			matrix[u][v] = 1;			
		}
	}
	
//显示邻接矩阵 
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = 0; j < n; j++)
//		{
//			if(j) cout << ' ';
//			cout << matrix[i+1][j+1];		
//		}
//		cout << endl;
//	}

	bfs_visit(1);

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



void bfs_visit(int u)
{
	
	//dis[]初始化，取值NIL时表示未访问 
	for (int i = 1; i <= n; i++)
		dis[i] = NIL;	
	dis[u] = 0;	
	
	queue<int> Q;
	Q.push(u);
	
//	int d = 1;
//	while (!Q.empty())
//	{
//		u = next(Q.front());
//
//		if (u)
//		{
//			dis[u] = d; 
//			Q.push(u);
//		}
//		else
//		{
//			Q.pop();			
//		}
//		
//		//判断是否进入下一层 
//		if (dis[Q.front()] == d) d++;
//
//	}

//另一种实现 
	while (!Q.empty())
	{
		u = Q.front();
		Q.pop();
		for (int v = 1; v <= n; v++)
		{
			if (matrix[u][v] != 0 && dis[v] == NIL)
			{
				dis[v] = dis[u] + 1;
				Q.push(v);
			}
		}
	} 
}


int next(int u)
{
	for (int i = 1; i <= n; i++)
	{
		if (matrix[u][i] != 0 && dis[i] == NIL)
			return i;
	}
	return 0;
}

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

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,v,k,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++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map> 
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#include <sstream>
#include <complex>
#define BIT(nr) (1ULL << (nr))
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807	
#define LL_HALFMAX 9223372036854775807 / 2	
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

using namespace std;

using Weight = int;
using Flow = int;
struct Edge {
	int src, dst;

	// libalgo のものに追加、メンバを追加するだけなので互換性は崩さないはず、逆辺のG[e.dstの]インデックスを保持
	int rev;
	Weight weight;
	Flow cap;
	Edge() : src(0), dst(0), weight(0) {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
	bool operator<(const Edge& right) const {
		return dst < right.dst;
	}
};

using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph& g, int a, int b, Weight w = 1) {
	g[a].emplace_back(a, b, w);
	g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

int dx[4] = { 0, 1, 0, -1 }; // x軸方向への変位
int dy[4] = { 1, 0, -1, 0 }; // y軸方向への変位

signed main() {
	int n;
	cin >> n;
	vector<int> dist(n + 1, -1);

	Graph G(n + 1);
	rep(i, n) {

		int u, k;
		cin >> u >> k;
		rep(j, k) {
			int v;
			cin >> v;
			add_arc(G, u, v);
		}
	}

	queue<int> q;
	q.push(1);
	dist[1] = 0;
	while (!q.empty())
	{
		int node = q.front();
		q.pop();
		for (Edge e : G[node]) {
			if (dist[e.dst] == -1) {
				dist[e.dst] = dist[e.src] + 1;
				q.push(e.dst);
			}
		}
	}

	REPS(i, n) {
		cout << i << " " << dist[i] << "\n";
	}

	return 0;
}

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

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

	std::vector<std::vector<int>> edges(n);
	for (auto&& v : edges) {
		int k;
		std::cin >> k >> k;

		for (int i = 0; i < k; ++i) {
			int u;
			std::cin >> u;
			--u;

			v.push_back(u);
		}
	}

	std::vector<int> depth(n, -1);
	depth[0] = 0;

	std::queue<int> que;
	que.push(0);
	while (!que.empty()) {
		int cur = que.front();
		que.pop();

		for (auto&& dist : edges[cur]) {
			if (depth[dist] == -1) {
				depth[dist] = depth[cur] + 1;
				que.push(dist);
			}
		}
	}

	for (int i = 0; i < n; ++i)
		std::cout << i + 1 << " " << depth[i] << std::endl;
}