Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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;

    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
    {
      if (cnrvvnAdjcy[nx][ny] == 1  &&
          rvoDist[nx].m_nFromMeTo == 0)
      {
        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);
      }
    }
    rvoDist[nx].m_nFromMeTo = 1;
  }
}

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

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

using namespace std;


int main() {
    int n, u, k; cin >> n;
    vector<vector<int> > v(n);
    for (int i = 0; i < n; ++i) {
        cin >> u >> k;
        vector<int> v0(k);
        for (int j = 0; j < k; ++j) {
            cin >> v0[j];
            --v0[j];
        }
        v[u-1] = v0;
    }
    queue<pair<int, int> > q;
    vector<bool> visited(n, false);
    q.push(make_pair(0, 0));
    visited[0] = true;
    vector<int> ans(n, -1);
    ans[0] = 0;
    while(!q.empty()) {
        pair<int, int> x = q.front(); q.pop();
        int cnt = x.second + 1;
        for (int i = 0; i < v[x.first].size(); ++i) {
            int j = v[x.first][i];
            if (!visited[j]) {
                q.push(make_pair(j, cnt));
                visited[j] = true;
                ans[j] = cnt;
            }
        }
    }
    for (int i = 0; i < n; ++i) cout << i + 1 << " " << ans[i] << endl;
    return 0;
}

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

using namespace std;

const static int N=100;
const static int 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[u]!=0){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;
    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; k++){
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main(){
  queue<int> q;
  int n,a,k,u,c[101];
  vector<int>  d[101];
  cin>>n;for(int i=0;i<n;i++){
    cin>>u>>k;c[i]=-1;
    d[u].resize(k);
    for(int j=0;j<k;j++) cin>>d[u][j];
  }q.push(1),c[1]=0;
  while(!q.empty()){
   a=q.front(),q.pop();
    for(int i=0;i<d[a].size();i++)
      if( d[a][i]>1 && c[d[a][i]]==-1) q.push(d[a][i]),c[d[a][i]]=c[a]+1;
  } for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

#define NO -1

using namespace std;


int adjacencyList[MAX + 1][MAX + 1];
int color[MAX + 1];
int d[MAX + 1];
int n;



void bfs(int o)
{
	queue<int> S;
	S.push(o);
	color[o] = GRAY;
	d[o] = 0;

	int fr;

	while (S.size() > 0) {
		fr = S.front();
		S.pop();
		color[fr] = BLACK;
		for (int i = 1; i <= n; i++) {
			if (color[i] == WHITE && adjacencyList[fr][i] != NO) {
				S.push(i);
				color[i] = GRAY;
				d[i] = d[fr] + 1;
			}
		}
	}

}

int main(void)
{

	int u;
	int k;
	int v;


	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			adjacencyList[i][j] = NO;
		}
	}

	for (int i = 0; i < n; i++) {
		cin >> u >> k;

		for (int j = 0; j < k; j++) {
			cin >> v;
			adjacencyList[u][v] = 1;
		}

	}

	bfs(1);

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


	return 0;
}

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

#define INFTY (1 << 30)

void BFS(bool mT[][100], int firstVerVal, int verNum, int distance[]) {
	queue<int>  verQ;
	verQ.push(firstVerVal);
	for (int i = 0; i < verNum; i++)
		distance[i] = INFTY;
	distance[firstVerVal] = 0;

	while (!verQ.empty()) {
		int curVerVal = verQ.front();
		verQ.pop();
		for (int i = 0; i < verNum; i++) {
			if (!mT[curVerVal][i] || distance[i] != INFTY) continue;
			distance[i] = distance[curVerVal] + 1;
			verQ.push(i);
		}
	}
}

int main() {
	bool matrixTable[100][100];
	int distance[100];
	int verNum;
	cin >> verNum;

	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++)
			matrixTable[i][j] = false;
		
	for (int i = 0; i < verNum; i++) {
		int anyVerVal, anyVerNum;
		cin >> anyVerVal >> anyVerNum;
		for (int j = 0; j < anyVerNum; j++) {
			int verValue; cin >> verValue;
			matrixTable[anyVerVal - 1][verValue - 1] = true;
		}
	}

	BFS(matrixTable, 0, verNum, distance);

	for (int i = 0; i < verNum; i++) {
		cout << i + 1 << " ";
		if (distance[i] == INFTY)
			cout << -1 << endl;
		else
			cout << distance[i] << endl;
	}

	return 0;
}