#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <complex>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cassert>
#include <iomanip>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putbd cout << "---------------------------------------------" << endl;
#define putln(x) cout << x << endl;
#define debug(x) cerr << #x << "=" << x << endl;
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector <int> vec;
typedef vector <ll> vecll;
typedef vector <double> vecd;
typedef vector <vec> mat;
typedef vector <vecll> matll;
typedef vector <vecd> matd;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 200020;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const ll INFL = 1e18;
const ll MOD = 1000000007;

const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
//const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1};

//********************************template END****************************************//

struct UnionFind
{
	//number of parents(par[i] = i => root)
	vector <int> par;
	//rank of tree
	vector <int> rank;

	UnionFind(int n) : par(n), rank(n, 1) {
		//initialize
		rep(n, i) par[i] = i;
	}

	//find root (keiroasshuku at the same time)
	int root(int x){
		if (par[x] == x) return x;
		else return par[x] = root(par[x]);
	}

	//unite
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x == y) return;
		if (rank[x] > rank[y]) swap(x, y);
		par[x] = y;
		rank[y] += rank[x];
	}

	//same tree?
	bool same(int x, int y){
		return root(x) == root(y);
	}
};


void solve(void)
{
	int N, K, L;
	cin >> N >> K >> L;

	UnionFind uf1(N), uf2(N);

	while(K--)
	{
		int u, v;
		cin >> u >> v;
		uf1.unite(u-1, v-1);
	}

	while(L--)
	{
		int u, v;
		cin >> u >> v;
		uf2.unite(u-1, v-1);
	}

	map<intP, int> mp;

	rep(N, i)
	{
		mp[intP(uf1.root(i), uf2.root(i))]++;
	}

	rep(N, i)
	{
		cout << mp[intP(uf1.root(i), uf2.root(i))] << (i == N-1 ? '\n' : ' ');
	}


}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define N 100005
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1 == p2)?EOF:*p1++)
using namespace std ;
//char buf[(1 << 22)] , *p1 = buf , *p2 = buf ;
inline int read ()
{
    char c = getchar () ; int x = 0 , f = 1 ;
    while (c < '0' || c > '9') { if (c == '-') f = -1 ; c = getchar () ; }
    while (c >= '0' && c <= '9'){ x = x * 10 + c - '0' ; c = getchar () ; }
    return x * f ;
}
map <int , int> mp[N << 3] ; //mp[u][w]:u属性为w的虚点编号
vector <pair <int , int> > edge[N << 3] ;
int dis[N << 3] , vis[N << 3] , n , m ;
queue <int> q ;
void spfa (int s)
{
    int tim = 0 ;
    for (int i = 0 ; i <= (N << 3) - 5 ; i ++) dis[i] = 1e9 ;
	// fill (dis , dis + (N << 2) , 1e9) ;
	dis[s] = 0 ;
	vis[s] = 1 ;
	q.push (s) ;
	while (! q.empty ())
	{
        // printf ("The %d time\n" , ++ tim) ;
		int x = q.front () ;
		q.pop () ;
		for (int i = 0 ; i < edge[x].size () ; i ++)
		{
			int y = edge[x][i].first ;
			if (dis[y] > dis[x] + edge[x][i].second)
			{
				dis[y] = dis[x] + edge[x][i].second ;
				if (! vis[y])
				{
					vis[y] = 1 ;
					q.push (y) ;
				}
			}
		}
        vis[x] = 0 ;
	}
}
int main ()
{
//    freopen (".in" , "r" , stdin) ; freopen (".out" , "w" , stdout) ;
	n = read () , m = read () ;
    int tot = n ;
	for (int i = 1 ; i <= m ; i ++)
	{
		int u = read () , v = read () , w = read () ;
        if (! mp[u].count (w))
        {
            mp[u][w] = ++ tot ;
            edge[u].push_back (make_pair (tot , 1)) ;
            edge[tot].push_back (make_pair (u , 1)) ;
        }
        if (! mp[v].count (w))
        {
            mp[v][w] = ++ tot ;
            edge[v].push_back (make_pair (tot , 1)) ;
            edge[tot].push_back (make_pair (v , 1)) ;
        }
        edge[mp[u][w]].push_back (make_pair (mp[v][w] , 0)) ;
        edge[mp[v][w]].push_back (make_pair (mp[u][w] , 0)) ;
    }
    spfa (1) ;
    if (dis[n] == 1e9) return puts ("-1") , 0 ;
    cout << dis[n] / 2 ;
}
