#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e3+9;
const int MAX=6e5+9;
const int MAX1=5e6+9;
const int INF=0x3f3f3f3f;
const double ep=1e-8;
const double ep1=1e-4;
const int mod=998244353;
const double inf=1e20;
const double pi=acos(-1);
#define debug1 puts("?");
#define debug(x) cout<<"##"<<(x)<<endl;
#define mk make_pair
#define PII pair<int,int>
#define PDI pair<double,int>
#define PIII pair<int,PII >
#define PIII1 pair<PII,int>
#define PIIII pair<PII,PII >
#define PIL pair<int,ll>
#define PLI pair<ll,int>
#define PLIII pair<PLI,PII >
#define PLL pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define sd(x) scanf("%d",&(x))
#define slld(x) scanf("%lld",&(x))
#define sdd(x,y) scanf("%d%d",&(x),&(y))
#define sc(s) scanf("%s",(s))
#define pd(x) printf("%d\n",(x))
#define plld(x) printf("%lld\n",(x))
#define pdk(x) printf("%d ",(x))
#define plldk(x) printf("%lld ",(x))
#define pdd(x,y) printf("%d %d\n",(x),(y))
#define PR(x) printf("Case %d: ",(x))
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
/*
*/
struct node{
	int to,nex;
	ll val;
}a[maxn];
int n,m;
int head[maxn],cnt;
void add(int fro,int to,ll val)
{
	a[++cnt].to=to;
	a[cnt].val=val;
	a[cnt].nex=head[fro];
	head[fro]=cnt;
}
ll d[maxn];
void bfs(int x)
{
	priority_queue<PLI,deque<PLI >,greater<PLI > >q;
	q.push(mk(0,x));
	bool ju=0;
	while(!q.empty())
	{
		PLI p=q.top();q.pop();
		int cur=p.se;
		ll val=p.fi;
		if(cur==x&&val)
		{
			plld(val);
			return;
		}
//		cout<<cur<<' '<<val<<endl;
		for(int i=head[cur];i;i=a[i].nex)
		{
			int to=a[i].to;
//			cout<<to<<' '<<a[i].val<<' '<<d[to]<<endl;
			if(val+a[i].val<d[to])
			{
				d[to]=val+a[i].val;
				q.push(mk(d[to],to));
//				if(to==x)ju=1;
			}
		}
	}
	puts("-1");
}
void solve()
{
	sdd(n,m);
	rep(i,1,m)
	{
		int u,v;
		ll w;
		sdd(u,v);
		slld(w);
		add(u,v,w);
	}
	rep(i,1,n)
	{
		memset(d,INF,sizeof d);
		bfs(i);
	}
}
int main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(0);
    int T=1;
//    sd(T);
    while(T--)solve(); 
}

#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <queue>
#include <queue>
#include <stack> 
#include <sstream>
#include <set>

typedef  long long ll;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = { 0, 1, 0,-1};
const int INF = 1e9;
#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(),a.end())
#define REVERSE(a) reverse(a.begin(),a.end())
int guki (int a){
    if(a%2==0) return 0;
    else return 1;
}
int gcd(int a, int b){
    if(a%b==0){
        return b;
    }else{
        return (gcd(b,a%b));
    }
}
int lcm(int a, int b){
    int x = gcd(a,b);
    return (a*b/x);
}

using namespace std;
// now

struct UnionFind {
    vector<int> d;
    UnionFind(int n=0) : d(n,-1){}
    int Find (int x){//根の番号を探す
        if(d[x] < 0) return x;
        return d[x] = Find(d[x]);
    }
    bool unite (int x, int y){
        x = Find(x); y = Find(y);
        if(x == y ) return false;
        if(d[x] < d[y]){
            swap(x,y);
        }
        d[x] += d[y];
        d[y] = x;
        return true;
    } 

    bool same(int x, int y){
        return Find(x) == Find(y);
    }
    int size(int x) {
        return (-d[Find(x)]);
    }
};


ll nCr (ll n, ll r){
    ll ans = 1;
    for(ll i = n ; i > n-r ; i --){
        ans *= i;
    }
    for(ll i = 1 ; i<= r; i ++){
        ans /= i;
    }
    return ans ;
}

int nPr (int n,int r){
    int ans = 1;
    for(int i = n ; i > n-r ; i --){
        ans *= i;
    }
    return ans ;
}




int main(){
    int n,m;
    cin >> n >> m;
    map<int,int> mp;

    REP(i,n){
        int x;
        cin >> x;

        mp[x]++;
    }

    REP(i,m){
        int x;
        cin >> x;

        mp[x]++;
    }

    for(auto x:mp){
        if(x.second != 2){
            cout << x.first << " ";
        }
    }
    cout << endl;
}

//500000500000
//963761198400
//4500001500000
//505013002500

