#include<bits/stdc++.h>
#define LL long long
using namespace std;

int n,m,a,b,t, d[100005];
int total = 0, ans = 1<<28,pathCost = 1<<28;
bool v[100005],belongsPath[200005] = {0};
int from[200005], to[200005], nxt[200005], adj[200005], w[200005], e = 0;
vector<int> optimalPath,tmpPath;

struct node{
    int in,d;
    bool operator<(const node &x)const{
        return d > x.d;
    }
};

void add_edge(int a, int b, int type){
    from[e] = a; to[e] = b; w[e] = type;
    nxt[e] = adj[a];
    adj[a] = e++;
}

bool relax(int relax, int target){
    if(d[relax] + 1 < d[target]){ d[target] = d[relax]+1; return true;}
    return false;
}
void dijkstra(int src){
    memset(v,0,sizeof(v));
    for(int i=0; i<100005;i++) d[i] = 1<<28;
    priority_queue<node> pq;
    d[src] = 0;
    pq.push({src, d[src]});
    while(!pq.empty()){
        node relax_node = pq.top(); pq.pop();
        int rin =relax_node.in, rd = relax_node.d;
        if(v[relax_node.in]) continue; // being used to be relax node already
        if(rin == n) break; //reached target
        v[rin] = 1;
        for(int i=adj[rin]; i != -1;i = nxt[i]){
            if(relax(rin, to[i])) pq.push({to[i], d[to[i]]});
        }

    }
}

void dfs(int x, int c, vector<int> path){
    if(x == n){
        if(c < pathCost){ // stupid but just try to cut cost
            pathCost = c;
            optimalPath = path;
        }
        return;
    }
    for(int i=adj[x]; i!=-1;i=nxt[i]){
        if(d[to[i]] == d[x]+1){
            path.push_back(i);
            dfs(to[i], c + !w[i], path);
            path.pop_back();
        }
    }
}

int main() {
    memset(adj,-1,sizeof(adj));
    scanf("%d%d", &n,&m);
    for(int i=0; i<m;i++){
        scanf("%d%d%d", &a,&b,&t);
        add_edge(a,b,t);add_edge(b,a,t);
        total += t;
    }
    dijkstra(1);
    dfs(1,0, tmpPath);

    printf("%d\n", total - d[n] + 2*pathCost);
    for(int i=0; i<optimalPath.size();i++) belongsPath[optimalPath[i]] = 1;
    for(int i=0; i<e;i+=2){
        if(belongsPath[i] | belongsPath[i+1]){
            if(!w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }
        else{
            if(w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;

int n,m,a,b,t, d[100005];
int total = 0, ans = 1<<28,pathCost = 1<<28;
bool v[100005],belongsPath[200005] = {0};
int from[200005], to[200005], nxt[200005], adj[200005], w[200005], e = 0;
vector<int> optimalPath,tmpPath;

struct node{
    int in,d;
    bool operator<(const node &x)const{
        return d > x.d;
    }
};

void add_edge(int a, int b, int type){
    from[e] = a; to[e] = b; w[e] = type;
    nxt[e] = adj[a];
    adj[a] = e++;
}

bool relax(int relax, int target){
    if(d[relax] + 1 < d[target]){ d[target] = d[relax]+1; return true;}
    return false;
}
void dijkstra(int src){
    memset(v,0,sizeof(v));
    for(int i=0; i<100005;i++) d[i] = 1<<28;
    priority_queue<node> pq;
    d[src] = 0;
    pq.push({src, d[src]});
    while(!pq.empty()){
        node relax_node = pq.top(); pq.pop();
        int rin =relax_node.in, rd = relax_node.d;
        if(rin == n) break; //reached target
        if(v[rin]) continue; // being used to be relax node already
        v[rin] = 1;
        for(int i=adj[rin]; i != -1;i = nxt[i])
            if(relax(rin, to[i])) pq.push({to[i], d[to[i]]});
    }
}

void dfs(int x, int c){
    if(x == n){
        if(c < pathCost){ // stupid but just try to cut cost
            pathCost = c;
            optimalPath = tmpPath;
        }
        return;
    }
    for(int i=adj[x]; i!=-1;i=nxt[i]){
        if(d[to[i]] == d[x]+1 && c+!w[i] < pathCost){
            tmpPath.push_back(i);
            dfs(to[i], c + !w[i]);
            tmpPath.pop_back();
        }
    }
}

int main() {
    memset(adj,-1,sizeof(adj));
    scanf("%d%d", &n,&m);
    for(int i=0; i<m;i++){
        scanf("%d%d%d", &a,&b,&t);
        add_edge(a,b,t);add_edge(b,a,t);
        total += t;
    }
    dijkstra(1);
    dfs(1,0);

    printf("%d\n", total - d[n] + 2*pathCost);
    for(int i=0; i<optimalPath.size();i++) belongsPath[optimalPath[i]] = 1;
    for(int i=0; i<e;i+=2)
        if(belongsPath[i] | belongsPath[i+1]){
            if(!w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }
        else{
            if(w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }

    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;

int n,m,a,b,t, d[100005];
int total = 0, ans = 1<<28,pathCost = 1<<28;
bool v[100005],belongsPath[200005] = {0};
int from[200005], to[200005], nxt[200005], adj[200005], w[200005], e = 0;
vector<int> optimalPath,tmpPath;
priority_queue<node> pq;

struct node{
    int in,d;
    bool operator<(const node &x)const{
        return d > x.d;
    }
};

void add_edge(int a, int b, int type){
    from[e] = a; to[e] = b; w[e] = type;
    nxt[e] = adj[a]; adj[a] = e++;
}

bool relax(int relax, int target){
    if(d[relax] + 1 < d[target]){ d[target] = d[relax] + 1; return true;}
    return false;
}
void dijkstra(int src){
    memset(v,0,sizeof(v));
    for(int i=0; i<=n; i++) d[i] = 1<<28;
    d[src] = 0;
    pq.push({src, d[src]});
    while(!pq.empty()){
        int rin =pg.top().in, rd = pg.top().d; pq.pop();
        if(rin == n) break; //reached target
        if(v[rin]) continue; // being used to be relax node already
        v[rin] = 1;
        for(int i=adj[rin]; i != -1;i = nxt[i])
            if(relax(rin, to[i])) pq.push({to[i], d[to[i]]});
    }
}

void dfs(int x, int c){
    if(x == n){
        if(c < pathCost){ // stupid but just try to cut cost
            pathCost = c;
            optimalPath = tmpPath;
        }
        return;
    }
    for(int i=adj[x]; i!=-1;i=nxt[i]){
        if(d[to[i]] == d[x]+1 && c+!w[i] < pathCost){
            tmpPath.push_back(i);
            dfs(to[i], c + !w[i]);
            tmpPath.pop_back();
        }
    }
}

int main() {
    memset(adj,-1,sizeof(adj));
    scanf("%d%d", &n,&m);
    for(int i=0; i<m;i++){
        scanf("%d%d%d", &a,&b,&t);
        add_edge(a,b,t);add_edge(b,a,t);
        total += t;
    }
    dijkstra(1);
    dfs(1,0);

    printf("%d\n", total - d[n] + 2*pathCost);
    for(int i=0; i<optimalPath.size();i++) belongsPath[optimalPath[i]] = 1;
    for(int i=0; i<e;i+=2)
        if(belongsPath[i] | belongsPath[i+1]){
            if(!w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }
        else{
            if(w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }

    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;

int n,m,a,b,t, d[100005];
int total = 0, ans = 1<<28,pathCost = 1<<28, q[100005];
bool v[100005],belongsPath[200005] = {0};
int from[200005], to[200005], nxt[200005], adj[200005], w[200005], e = 0;
vector<int> optimalPath,tmpPath;

void add_edge(int a, int b, int type){
    from[e] = a; to[e] = b; w[e] = type;
    nxt[e] = adj[a]; adj[a] = e++;
}

void bfs(int src){
    for(int i=0; i<=n; i++) d[i] = 1<<28;
    int i,j;
    d[src] = i = j = 0;
    q[j++] = src;
    while(i<=j && q[i] != n){
        for(int k=adj[q[i]]; k!=-1; k=nxt[k]){
            if(d[from[k]] + 1 < d[to[k]]){
                d[to[k]] = d[from[k]] + 1;
                q[j++] = to[k];
            }
        }
        i++;
    }
}

void dfs(int x, int c){
    if(x == n){
        if(c < pathCost){ // stupid but just try to cut cost
            pathCost = c;
            optimalPath = tmpPath;
        }
        return;
    }
    for(int i=adj[x]; i!=-1;i=nxt[i]){
        if(d[to[i]] == d[x]+1 && c+!w[i] < pathCost){
            tmpPath.push_back(i);
            dfs(to[i], c + !w[i]);
            tmpPath.pop_back();
        }
    }
}

int main() {
    memset(adj,-1,sizeof(adj));
    scanf("%d%d", &n,&m);
    for(int i=0; i<m;i++){
        scanf("%d%d%d", &a,&b,&t);
        add_edge(a,b,t);add_edge(b,a,t);
        total += t;
    }
    bfs(1);
    dfs(1,0);

    printf("%d\n", total - d[n] + 2*pathCost);
    for(int i=0; i<optimalPath.size();i++) belongsPath[optimalPath[i]] = 1;
    for(int i=0; i<e;i+=2)
        if(belongsPath[i] | belongsPath[i+1]){
            if(!w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }
        else{
            if(w[i]) printf("%d %d %d\n", from[i], to[i], !w[i]);
        }

    return 0;
}


#include <bits\stdc++.h>
using namespace std;

const int maxn=100010;

typedef pair<int,int> pr;

int n, m;
map<pr,int> M;
vector<int> G[maxn];
vector<pr> bin;

int h[maxn], vis[maxn], cnt[maxn], pre[maxn];

void g(){
    queue<pr> Q;
    memset(vis, 0, sizeof vis);
    memset(h, 0, sizeof h);
    Q.push(pr(1,0));
    vis[1]=1;
    for(int u=0; u!=n; ){
        pr x=Q.front(); Q.pop();
        u=x.first;
        h[u]=x.second;
        for(int i=0; i<G[u].size(); i++){
            int v=G[u][i];
            if(!vis[v]) Q.push(pr(v,h[u]+1)), vis[v]=1;
        }
    }
}

int f(){
    queue<int> Q;
    memset(cnt, -1, sizeof cnt);
    cnt[1]=0;
    Q.push(1);
    for(int u=0; u!=n; ){
        u=Q.front(); Q.pop();
        for(int i=0; i<G[u].size(); i++){
            int v=G[u][i];
            if(h[u]<h[v]){
                int t=cnt[u]+M[pr(min(u,v),max(u,v))];
                if(t>cnt[v]){
                    cnt[v]=t, pre[v]=u;
                    Q.push(v);
                }
            }
        }
    }
    int x=0;
    for(int u=n, v; u>1; u=v){
        v=pre[u];
        int a=min(u,v), b=max(u,v);
        if(!M[pr(a,b)]) bin.push_back(pr(a,b));
        else            M[pr(a,b)]=0, x++;
    }
    for(map<pr,int>::iterator it=M.begin(); it!=M.end(); it++)
        if(it->second) bin.push_back(it->first), x++;
    return x+h[n]-2*cnt[n];
}

int main(){
    #ifdef LOCAL
    freopen("in22.txt", "r", stdin);
    #endif
    while(cin>>n>>m){
        M.clear();
        bin.clear();
        for(int i=1; i<=n; i++)
            G[i].clear();
        for(int i=0; i<m; i++){
            int a, b, c;
            cin>>a>>b>>c;
            G[a].push_back(b);
            G[b].push_back(a);
            if(a>b) swap(a,b);
            M[pr(a,b)]=c;
        }
        g();
        cout<<f()<<endl;
        for(int i=0; i<bin.size(); i++)
            cout<<bin[i].first<<" "<<bin[i].second<<" "<<!M[bin[i]]<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;
//****************************************
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<PII> VPII;
typedef vector<string> VS;
typedef vector<bool> VB;
//****************************************

int n, m;
vector<VPII> g;
int working = 0;

struct road {
   int a, b, z;
};
vector<road> roads;

void compute() {
   VI dist(n+1, -1), cost(n+1, n), parent(n+1);
   queue<int> q;
   q.push(1);
   dist[1] = 0;
   cost[1] = 0;
   while(!q.empty()) {
      int u = q.front(); q.pop();
      for(auto &v : g[u]) {
         int c = cost[u] + !v.second;
         int d = dist[u] + 1;
         if(dist[v.first] == -1) {
            q.push(v.first);
            dist[v.first] = d;
         }
         if(dist[v.first] == d && c < cost[v.first]) {
            cost[v.first] = c;
            parent[v.first] = u;
         }
      }
   }
   cout << working - dist[n] + 2*cost[n] << endl;
   VB onPath(n+1, false);
   for(int i = n; i >= 1; i = parent[i])
      onPath[i] = true;
   for(road &r : roads) {
      if(onPath[r.a] && onPath[r.b]) {
         if(r.z == 0)
            cout << r.a << " " << r.b << " 1\n";
      }
      else if(r.z == 1)
         cout << r.a << " " << r.b << " 0\n";
   }
}

int main() {
   ios::sync_with_stdio(false);
   cin >> n >> m;
   g.resize(n+1);
   for(int i = 0,a,b,z; i < m; ++i) {
      cin >> a >> b >> z;
      g[a].push_back({b, z});
      g[b].push_back({a, z});
      roads.push_back({a, b, z});
      if(z) ++working;
   }
   compute();
   return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;
//****************************************
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<PII> VPII;
typedef vector<string> VS;
typedef vector<bool> VB;
//****************************************

int n, m;
vector<VPII> g;
VI w, d, pre;
VB visited;
int working = 0;

struct road {
   int a, b, z;
};
vector<road> r;

void compute() {
   d.assign(n+1, 0);
   w.assign(n+1, 0);
   pre.assign(n+1, 0);
   visited.assign(n+1, false);
   queue<int> q;
   q.push(1);
   visited[1] = true;
   while(!q.empty()) {
      int u = q.front(); q.pop();
      for(auto &v : g[u]) {
         if(visited[v.first]) {
            if(d[v.first] == d[u] + 1 && w[u] + !v.second < w[v.first]) {
               w[v.first] = w[u] + !v.second;
               pre[v.first] = u;
            }
         }
         else {
            q.push(v.first);
            visited[v.first] = true;
            w[v.first] = w[u] + !v.second;
            d[v.first] = d[u] + 1;
            pre[v.first] = u;
         }
      }
   }
   cout << working - d[n] + 2*w[n] << endl;
   VI path;
   VB in(n+1, false);
   for(int i = n; i >= 1; ) {
      path.push_back(i);
      in[i] = true;
      //cout << i << " ";
      i = pre[i];
   }
   //cout << endl;
   for(road &x : r) {
      //cout << x.a << " " << x.b << " " << x.z << endl;
      if(in[x.a] && in[x.b]) {
         if(x.z == 0)
            cout << x.a << " " << x.b << " 1" << endl;
      }
      else if(x.z == 1)
         cout << x.a << " " << x.b << " 0" << endl;
   }
}

int main() {
   ios::sync_with_stdio(false);
   cin >> n >> m;
   g.resize(n+1);
   for(int i = 0,a,b,z; i < m; ++i) {
      cin >> a >> b >> z;
      g[a].push_back({b, z});
      g[b].push_back({a, z});
      r.push_back({a, b, z});
      if(z) ++working;
   }
   compute();
   return 0;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <queue>
#include <vector>
using namespace std;
struct Road{ int u; int v; int z; };
list <int> adjoin[100001];
Road roads[100001] = {};
int dis[100001] = {};
int g[100001] = {};
int f[100001] = {};
int vis[100001] = {};
int out[100001] = {};
int path[100001];
int M, N;
struct  comp
{
	bool operator ()(int x, int y)
	{
		if (dis[x] == dis[y])
			return g[x] < g[y];
		else
			return dis[x] < dis[y];
	}
};
int bfs()
{
	priority_queue <int, vector<int>, comp> qu;
	int key=1, u, v,t,z;
	list <int>::iterator it, end;
	vis[1] = 1;
	qu.push(1);
	while (key&&!qu.empty())
	{
		u = qu.top();
		qu.pop();
		if (out[u])
			continue;
		else
			out[u] = 1;
		end = adjoin[u].end();
		for (it = adjoin[u].begin(); it != end; ++it)
		{
			t = *it;
			v = (u != roads[t].u ? roads[t].u : roads[t].v);
			z = g[u] + (roads[t].z ? -1 : 1);
			if (!vis[v] || dis[v] > dis[u]+1 || dis[v] == dis[u] + 1 && g[v] > z)
			{
				vis[v] = 1;
				dis[v] = dis[u] + 1;
				g[v] = z;
				f[v] = t;
				qu.push(v);
			}
		}
	}
	return 0;
}
int repath()
{
	int u, v,t;
	int i = 0;
	u = N;
	while (u!=1)
	{
		t = f[u];
		path[i++] = f[u];
		u = (u != roads[t].u ? roads[t].u : roads[t].v);
	}
	int s = i;
	for (i = 0; i < s; ++i)
	if (roads[path[i]].z == 1)
		roads[path[i]].z = 0;
	else
		roads[path[i]].z = 2;

	return 0;
}
int main()
{
	int u, v, z;
	int i, j;
	int s = 0;
	cin >> N >> M;
	for (i = 0; i < M; ++i)
	{
		cin >> u >> v >> z;
		if (z)
			++s;
		roads[i].u = u;
		roads[i].v = v;
		roads[i].z = z;
		adjoin[u].push_back(i);
		adjoin[v].push_back(i);
	}
	g[1] = s;
	bfs();
	repath();
	cout << g[N]<<endl;
	for (i = 0; i < M;++i)
	if (roads[i].z)
		cout << roads[i].u << " " << roads[i].v <<" "<< (roads[i].z==2)<<endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <list>
#include <queue>
#include <vector>
using namespace std;
struct Road{ int u; int v; int z; };
list <int> adjoin[100001];
Road roads[100001] = {};
int dis[100001] = {};
int g[100001] = {};
int f[100001] = {};
int vis[100001] = {};
int out[100001] = {};
int path[100001];
int M, N;
int comp1(Road x,Road y)
{
	if (x.u == y.u)
		return x.v < y.v;
	else
		return x.u < y.u;
}
struct  comp
{
	bool operator ()(int x, int y)
	{
		if (dis[x] == dis[y])
			return g[x] < g[y];
		else
			return dis[x] < dis[y];
	}
};
int bfs()
{
	queue <int> qu;
	int key=1, u, v,t,z;
	list <int>::iterator it, end;
	vis[1] = 1;
	qu.push(1);
	while (key&&!qu.empty())
	{
		u = qu.front();
		qu.pop();
		if (out[u])
			continue;
		else
			out[u] = 1;
		end = adjoin[u].end();
		for (it = adjoin[u].begin(); it != end; ++it)
		{
			t = *it;
			v = (u != roads[t].u ? roads[t].u : roads[t].v);
			z = g[u] + (roads[t].z ? -1 : 1);
			if (!vis[v] || dis[v] > dis[u]+1 || dis[v] == dis[u] + 1 && g[v] > z)
			{
				vis[v] = 1;
				dis[v] = dis[u] + 1;
				g[v] = z;
				f[v] = t;
				qu.push(v);
			}
		}
	}
	return 0;
}
int repath()
{
	int u, v,t;
	int i = 0;
	u = N;
	while (u!=1)
	{
		t = f[u];
		path[i++] = f[u];
		u = (u != roads[t].u ? roads[t].u : roads[t].v);
	}
	int s = i;
	for (i = 0; i < s; ++i)
	if (roads[path[i]].z == 1)
		roads[path[i]].z = 0;
	else
		roads[path[i]].z = 2;

	return 0;
}
int main()
{
	int u, v, z;
	int i, j;
	int s = 0;
	cin >> N >> M;
	for (i = 0; i < M; ++i)
	{
		cin >> u >> v >> z;
		if (u>v)
			swap(u, v);
		if (z)
			++s;
		roads[i].u = u;
		roads[i].v = v;
		roads[i].z = z;
	}
	sort(roads, roads + M, comp1);
	for (i = 0; i < M; ++i)
	{
		u = roads[i].u;
		v = roads[i].v;
		adjoin[u].push_back(i);
		adjoin[v].push_back(i);
	}
	g[1] = s;
	bfs();
	repath();
	cout << g[N]<<endl;
	for (i = 0; i < M;++i)
	if (roads[i].z)
		cout << roads[i].u << " " << roads[i].v <<" "<< (roads[i].z==2)<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<pair<int, int>> e[N];
int t[N];
char used[N];
pair<int, int> d[N], E[N];
int p1[N];
map<pair<int, int>, int> mp;

main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE

    int n, m;
    scanf("%d%d", &n, &m);

    for (int i = 0; i < m; i++) {
        int x, y;
        scanf("%d%d%d", &x, &y, &t[i]);
        x--; y--;
        E[i] = {x, y};
        mp[{x, y}] = mp[{y, x}] = i;
        e[x].push_back({y, i});
        e[y].push_back({x, i});
    }

    d[0] = {0, 0};
    vector<int> p(n, -1);

    for (int i = 1; i < n; i++) {
        d[i] = {1e9, 1e9};
    }

    set<pair<pair<int, int>, int>> s;
    s.insert({d[0], 0});

    for (; !s.empty(); s.erase(s.begin())) {
        int v = s.begin()->second;
        for (auto i : e[v]) {
            int to = i.first;
            pair<int, int> tmp = d[v];
            tmp.first++;
            tmp.second += 1 - t[i.second];
            if (d[to] <= tmp) {
                continue;
            }
            d[to] = tmp;
            p[to] = v;
            s.insert({d[to], to});
        }
    }

    for (int i = n - 1; ; ) {
        int j = p[i];
        if (j == -1) {
            break;
        }
        used[mp[{i, j}]] = true;
        i = j;
    }

    vector<pair<pair<int, int>, int>> ans;

    for (int i = 0; i < m; i++)
        if (used[i] && !t[i]) {
            ans.push_back({E[i], 1});
        } else if (!used[i] && t[i]) {
            ans.push_back({E[i], 0});
        }

    printf("%d\n", ans.size());

    for (auto i : ans) {
        printf("%d %d %d\n", i.first.first + 1, i.first.second + 1, i.second);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<pair<int, int>> e[N];
int t[N];
char used[N];
pair<int, int> d[N], E[N];
int p1[N];
map<pair<int, int>, int> mp;

main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE

    int n, m;
    scanf("%d%d", &n, &m);

    for (int i = 0; i < m; i++) {
        int x, y;
        scanf("%d%d%d", &x, &y, &t[i]);
        E[i] = {x, y};
        mp[{x, y}] = mp[{y, x}] = i;
        x--; y--;
        e[x].push_back({y, i});
        e[y].push_back({x, i});
    }

    d[0] = {0, 0};
    vector<int> p(n, -1);

    for (int i = 1; i < n; i++) {
        d[i] = {1e9, 1e9};
    }

    priority_queue<pair<pair<int, int>, int>> q;
    q.push({d[0], 0});

    for (; !q.empty(); q.pop()) {
        int v = q.top().second;
        for (auto i : e[v]) {
            int to = i.first;
            pair<int, int> tmp = d[v];
            tmp.first++;
            tmp.second += t[i.second];
            if (d[to] <= tmp) {
                continue;
            }
            d[to] = tmp;
            p[to] = v;
            q.push({d[to], to});
        }
    }

    for (int i = n - 1; i != -1; ) {
        int j = p[i];
        used[mp[{i, j}]] = true;
        i = j;
    }

    vector<pair<pair<int, int>, int>> ans;

    for (int i = 0; i < m; i++) {
        if (used[i] && t[i]) {
            ans.push_back({E[i], 0});
        } else if (!used[i] && !t[i]) {
            ans.push_back({E[i], 1});
        }
    }

    printf("%d\n", ans.size());

    for (auto i : ans) {
        printf("%d %d %d\n", i.first.first, i.first.second, i.second);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long

vector <pair<int, int>> v[100010];
pair<int, int> d[100010];
int p[100010];
set<pair<int, int>> s;
map<pair<int, int>, int> mp;
vector <pair<pair<int, int>, int>> ansv;

int main() {
//    freopen("input.txt", "r", stdin);
    int n, m, i, x, y, z, ans(0);
    cin >> n >> m;
    for(i = 1; i <= m; i++) {
        cin >> x >> y >> z;
        v[x].pb({y, 1 - z});
        v[y].pb({x, 1 - z});
        ans += z;

    }
    d[1] = {0, ans};
    for(i = 2; i <= n; i++) {
        d[i] = {1000000000, 0};
        s.insert({d[i].fi, i});
    }
    s.insert({0, 1});
   while(!s.empty()) {
        x = (*s.begin()).se;

        s.erase(s.begin());
        for(auto y: v[x]) {
            if(y.se == 0)
                y.se = -1;
            if(d[x].fi + 1 < d[y.fi].fi || (d[x].fi + 1 == d[y.fi].fi && d[x].se + y.se < d[y.fi].se)) {
                s.erase(s.find({d[y.fi].fi, y.fi}));
                d[y.fi].fi = d[x].fi + 1;
                d[y.fi].se = d[x].se + y.se;
                p[y.fi] = x;
                s.insert({d[y.fi].fi, y.fi});
            }
        }
    }
    i = n;
    s.clear();
    while(i > 1) {
        mp[{p[i], i}] = 1;
        mp[{i, p[i]}] = 1;
        i = p[i];
    }
    for(i = 1; i <= n; i++) {
        for(auto x: v[i]) {
            if(mp[{i, x.fi}] != 1 - x.se && i < x.fi) {
                ansv.pb({{i, x.fi}, x.se});
            }
        }
    }
    cout << ansv.size() << "\n";
    for(auto x: ansv)
        cout << x.fi.fi << ' ' << x.fi.se << ' ' << x.se << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define ll long long

ll dp[1010][110];
ll s[1010][110];

bool check(int i, int j) {
    while(i) {
        int ans = ans * 10 + i % 10;
        if(ans % j == 0)
            return 1;
        i /= 10;
    }
    return 0;
}

int main() {
    freopen("input.txt", "r", stdin);
    ll n, k, m, i, j, q, ost, x;
    cin >> n >> k >> m;
    for(i = 100; i  <= 999; i++) {
        dp[0][0] += check(i, k);
        if(check(i, k))
            cout << i << ' ';
    }
    cout << "\n\t";
    cout << dp[0][0] << "\n\n";
    for(i = 0; i <= 9; i++)
        dp[1][i % k] = (dp[1][i % k] + 1) % m;

    cout << dp[n][0] - (n == 1);
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;

const int INF = (int)1e9;
const int MAXN = 100005;
int head[MAXN];
int uu[MAXN];
int vv[MAXN];
int ww[MAXN];
int edgef[MAXN<<1];
int edge[MAXN<<1];
int id[MAXN<<1];
int next1[MAXN<<1];
int esz;
int n,m;
int vis[MAXN];
int dis[MAXN];
int dis2[MAXN];
int pre[MAXN];

void init()
{
    memset(head,-1,sizeof(head));
    esz=0;
}

void addedge(int u,int v,int w)
{
    edgef[esz]=u;
    edge[esz]=v;
    id[esz]=w;
    next1[esz]=head[u];
    head[u]=esz++;
}

struct node1{
    int u,w;
    node1(){}
    node1(int v1,int v2):u(v1),w(v2){}
};
queue<node1>q;

void spfa()
{
    for(int i=1;i<=n;i++)dis[i]=INF;
    dis[1]=0;
    q.push(node1(1,0));
    vis[1]=1;
    while(!q.empty()){
        node1 uu=q.front();q.pop();
        int u=uu.u;
        vis[u]=0;
        for(int i=head[u];i+1;i=next1[i]){
            int v=edge[i];
            int is=ww[id[i]];
            int to=uu.w+is;
            if(dis[u]+1<dis[v]){
                dis[v]=dis[u]+1;
                pre[v]=i;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
            else if(dis[u]+1==dis[v] && to>dis2[v]){
                pre[v]=i;
                dis2[v]=to;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
        }
    }
}

struct node{
    int a,b,c;
    node(){}
    node(int v1,int v2,int v3):a(v1),b(v2),c(v3){}
};

vector<node>ans;
int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&uu[i],&vv[i],&ww[i]);
        addedge(uu[i],vv[i],i);
        addedge(vv[i],uu[i],i);
    }
    spfa();
    memset(vis,0,sizeof(vis));
    int u=n;
    while(u!=1){
        int res=pre[u];
        vis[id[res]]=1;
        if(ww[id[res]]==0)
            ans.push_back(node(edgef[res],edge[res],1));
        u=edgef[res];
    }
    for(int i=0;i<m;i++){
        if(vis[i]==1)continue;
        if(ww[i]==1){
            ans.push_back(node(uu[i],vv[i],0));
        }
    }
    int L=ans.size();
    printf("%d\n",L);
    for(int i=0;i<L;i++)
        printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;

const int INF = (int)1e9;
const int MAXN = 100005;
int head[MAXN];
int uu[MAXN];
int vv[MAXN];
int ww[MAXN];
int edgef[MAXN<<1];
int edge[MAXN<<1];
int id[MAXN<<1];
int next1[MAXN<<1];
int esz;
int n,m;
int vis[MAXN];
int dis[MAXN];
int dis2[MAXN];
int pre[MAXN];
map<int,int>mp[MAXN];
map<int,int>::iterator it;

void init()
{
    memset(head,-1,sizeof(head));
    esz=0;
}

void addedge(int u,int v,int w)
{
    edgef[esz]=u;
    edge[esz]=v;
    id[esz]=w;
    next1[esz]=head[u];
    head[u]=esz++;
}

struct node1{
    int u,w;
    node1(){}
    node1(int v1,int v2):u(v1),w(v2){}
};
queue<node1>q;

void spfa()
{
    for(int i=1;i<=n;i++)dis[i]=INF;
    dis[1]=0;
    q.push(node1(1,0));
    vis[1]=1;
    while(!q.empty()){
        node1 uu=q.front();q.pop();
        int u=uu.u;
        vis[u]=0;
        for(int i=head[u];i+1;i=next1[i]){
            int v=edge[i];
            int is=ww[id[i]];
            int to=uu.w+is;
            if(dis[u]+1<dis[v]){
                dis[v]=dis[u]+1;
                pre[v]=i;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
            else if(dis[u]+1==dis[v] && to>dis2[v]){
                pre[v]=i;
                dis2[v]=to;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
        }
    }
}

struct node{
    int a,b,c;
    node(){}
    node(int v1,int v2,int v3):a(v1),b(v2),c(v3){}
};

vector<node>ans;
int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&uu[i],&vv[i],&ww[i]);
        addedge(uu[i],vv[i],i);
        addedge(vv[i],uu[i],i);
    }
    spfa();
    memset(vis,0,sizeof(vis));
    int u=n;
    while(u!=1){
        int res=pre[u];
        vis[id[res]]=1;
        if(ww[id[res]]==0){
            int u1=edgef[res];
            int v1=edge[res];
            if(u1>v1)swap(u1,v1);
            if(mp[u1].find(v1)==mp[u1].end()){
            mp[u1][v1]=1;
            ans.push_back(node(u1,v1,1));
            }
        }
        u=edgef[res];
    }
    for(int i=0;i<m;i++){
        if(vis[i]==1)continue;
        if(ww[i]==1){
            int u1=uu[i];
        int v1=vv[i];
        if(u1>v1)swap(u1,v1);
        if(mp[u1].find(v1)==mp[u1].end()){
            mp[u1][v1]=1;
            ans.push_back(node(uu[i],vv[i],0));
        }
        }
    }
    int L=ans.size();
    printf("%d\n",L);
    for(int i=0;i<L;i++)
        printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;

const int INF = (int)1e9;
const int MAXN = 100005;
int head[MAXN];
int uu[MAXN];
int vv[MAXN];
int ww[MAXN];
int edgef[MAXN<<1];
int edge[MAXN<<1];
int id[MAXN<<1];
int next[MAXN<<1];
int esz;
int n,m;
int vis[MAXN];
int dis[MAXN];
int dis2[MAXN];
int pre[MAXN];

void init()
{
    memset(head,-1,sizeof(head));
    esz=0;
}

void addedge(int u,int v,int w)
{
    edgef[esz]=u;
    edge[esz]=v;
    id[esz]=w;
    next[esz]=head[u];
    head[u]=esz++;
}

struct node1{
    int u,w;
    node1(){}
    node1(int v1,int v2):u(v1),w(v2){}
};
queue<node1>q;

void spfa()
{
    for(int i=1;i<=n;i++)dis[i]=INF;
    dis[1]=0;
    q.push(node1(1,0));
    vis[1]=1;
    while(!q.empty()){
        node1 uu=q.front();q.pop();
        int u=uu.u;
        vis[u]=0;
        for(int i=head[u];i+1;i=next[i]){
            int v=edge[i];
            int is=ww[id[i]];
            int to=uu.w+is;
            if(dis[u]+1<dis[v]){
                dis[v]=dis[u]+1;
                pre[v]=i;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
            else if(dis[u]+1==dis[v] && to>dis2[v]){
                pre[v]=i;
                dis2[v]=to;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
        }
    }
}

struct node{
    int a,b,c;
    node(){}
    node(int v1,int v2,int v3):a(v1),b(v2),c(v3){}
};

vector<node>ans;
int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&uu[i],&vv[i],&ww[i]);
        addedge(uu[i],vv[i],i);
        addedge(vv[i],uu[i],i);
    }
    spfa();
    memset(vis,0,sizeof(vis));
    int u=n;
    while(u!=1){
        int res=pre[u];
        vis[id[res]]=1;
        if(ww[id[res]]==0)
            ans.push_back(node(edgef[res],edge[res],1));
        u=edgef[res];
    }
    for(int i=0;i<m;i++){
        if(vis[i]==1)continue;
        if(ww[i]==1){
            ans.push_back(node(uu[i],vv[i],0));
        }
    }
    int L=ans.size();
    printf("%d\n",L);
    for(int i=0;i<L;i++)
        printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;

const int INF = (int)1e9;
const int MAXN = 100005;
int head[MAXN];
int uu[MAXN];
int vv[MAXN];
int ww[MAXN];
int edgef[MAXN<<1];
int edge[MAXN<<1];
int id[MAXN<<1];
int next1[MAXN<<1];
int esz;
int n,m;
int vis[MAXN];
int dis[MAXN];
int dis2[MAXN];
int pre[MAXN];
map<int,int>mp[MAXN];
map<int,int>::iterator it;

void init()
{
    memset(head,-1,sizeof(head));
    esz=0;
}

void addedge(int u,int v,int w)
{
    edgef[esz]=u;
    edge[esz]=v;
    id[esz]=w;
    next1[esz]=head[u];
    head[u]=esz++;
}

struct node1{
    int u,w;
    node1(){}
    node1(int v1,int v2):u(v1),w(v2){}
};
queue<node1>q;

void spfa()
{
    for(int i=1;i<=n;i++)dis[i]=INF;
    dis[1]=0;
    q.push(node1(1,0));
    vis[1]=1;
    while(!q.empty()){
        node1 uu=q.front();q.pop();
        int u=uu.u;
        vis[u]=0;
        for(int i=head[u];i+1;i=next1[i]){
            int v=edge[i];
            int is=ww[id[i]];
            int to=uu.w+is;
            if(dis[u]+1<dis[v]){
                dis[v]=dis[u]+1;
                pre[v]=i;
                dis2[v]=to;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
            else if(dis[u]+1==dis[v] && to>dis2[v]){
                pre[v]=i;
                dis2[v]=to;
                if(vis[v]==1)continue;
                q.push(node1(v,to));
            }
        }
    }
}

struct node{
    int a,b,c;
    node(){}
    node(int v1,int v2,int v3):a(v1),b(v2),c(v3){}
};

vector<node>ans;
int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&uu[i],&vv[i],&ww[i]);
        addedge(uu[i],vv[i],i);
        addedge(vv[i],uu[i],i);
    }
    spfa();
    memset(vis,0,sizeof(vis));
    int u=n;
    while(u!=1){
        int res=pre[u];
        vis[id[res]]=1;
        if(ww[id[res]]==0){
            int u1=edgef[res];
            int v1=edge[res];
            if(u1>v1)swap(u1,v1);
            if(mp[u1].find(v1)==mp[u1].end()){
            mp[u1][v1]=1;
            ans.push_back(node(u1,v1,1));
            }
        }
        u=edgef[res];
    }
    for(int i=0;i<m;i++){
        if(vis[i]==1)continue;
        if(ww[i]==1){
            int u1=uu[i];
        int v1=vv[i];
        if(u1>v1)swap(u1,v1);
        if(mp[u1].find(v1)==mp[u1].end()){
            mp[u1][v1]=1;
            ans.push_back(node(uu[i],vv[i],0));
        }
        }
    }
    int L=ans.size();
    printf("%d\n",L);
    for(int i=0;i<L;i++)
        printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int INF = 0x7fffffff;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
struct edge {
    int v, c;
    edge() {}
    edge(int v, int c): v(v), c(c) {}
};
map<pair<int, int>, int> M;
vector<edge> G[N];
queue<int> Q;
int n, m, x[N], y[N], c[N];
int dis[N], dp[N], pre[N];
int ax[N], ay[N], ac[N];
bool vis[N];

int main() {
#ifdef Tally_Ho
    freopen("in.txt", "r", stdin);
#endif // Tally_Ho
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d", &x[i], &y[i], &c[i]);
        G[x[i]].push_back(edge(y[i], c[i]));
        G[y[i]].push_back(edge(x[i], c[i]));
    }
    memset(pre,-1,sizeof(pre));
    Q.push(1);
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].v;
            int c = G[u][i].c; //1:working
            if(!vis[v]) {
                dis[v] = dis[u] + 1;
                vis[v] = 1;
                Q.push(v);
            }
            if(dis[v] == dis[u] + 1 && dp[u] + c >= dp[v]) {
                pre[v] = u;
                dp[v] = dp[u] + c;
            }
        }
    }
    vector<int > ans;
    int v = n;
    while(v != 1) {
        M[make_pair(v, pre[v])] = 1;
        v = pre[v];
    }
    int len = 0;
    for(int i = 0; i < m; i++) {
        if(M[make_pair(x[i], y[i])] == 1 || M[make_pair(y[i], x[i])] == 1 && c[i] == 0) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 1;
        } else if(M[make_pair(x[i], y[i])] == 0 && M[make_pair(y[i], x[i])] == 0 && c[i] == 1) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 0;
        }
    }
    printf("%d\n",len);
    for(int i=0;i<len;i++)
        printf("%d %d %d\n",ax[i],ay[i],ac[i]);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int INF = 0x7fffffff;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
struct edge {
    int v, c;
    edge() {}
    edge(int v, int c): v(v), c(c) {}
};
map<pair<int, int>, int> M;
vector<edge> G[N];
queue<int> Q;
int n, m, x[N], y[N], c[N];
int dis[N], dp[N], pre[N];
int ax[N], ay[N], ac[N];
bool vis[N];

int main() {
#ifdef Tally_Ho
    freopen("in.txt", "r", stdin);
#endif // Tally_Ho
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d", &x[i], &y[i], &c[i]);
        G[x[i]].push_back(edge(y[i], c[i]));
        G[y[i]].push_back(edge(x[i], c[i]));
    }
    Q.push(1);
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].v;
            int c = G[u][i].c; //1:working
            if(!vis[v]) {
                dis[v] = dis[u] + 1;
                vis[v] = 1;
                Q.push(v);
            }
            if(dis[v] == dis[u] + 1 && dp[u] + c >= dp[v]) {
                pre[v] = u;
                dp[v] = dp[u] + c;
            }
        }
    }
    vector<int > ans;
    int v = n;
    while(v != 1) {
        M[make_pair(v, pre[v])] = 1;
        v = pre[v];
    }
    int len = 0;
    for(int i = 0; i < m; i++) {
        bool onRoad = M[make_pair(x[i], y[i])] || M[make_pair(y[i], x[i])];

        if(onRoad && c[i] == 0) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 1;
        } else if(!onRoad && c[i] == 1) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 0;
        }
    }
    printf("%d\n", len);
    for(int i = 0; i < len; i++)
        printf("%d %d %d\n", ax[i], ay[i], ac[i]);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int INF = 0x7fffffff;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
struct edge {
    int v, c;
    edge() {}
    edge(int v, int c): v(v), c(c) {}
};
map<pair<int, int>, int> M;
vector<edge> G[N];
queue<int> Q;
int n, m, x[N], y[N], c[N];
int dis[N], dp[N], pre[N];
int ax[N], ay[N], ac[N];
bool vis[N];

int main() {
#ifdef Tally_Ho
    freopen("in.txt", "r", stdin);
#endif // Tally_Ho
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d", &x[i], &y[i], &c[i]);
        G[x[i]].push_back(edge(y[i], c[i]));
        G[y[i]].push_back(edge(x[i], c[i]));
    }
    memset(pre,-1,sizeof(pre));
    Q.push(1);
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].v;
            int c = G[u][i].c; //1:working
            if(!vis[v]) {
                dis[v] = dis[u] + 1;
                vis[v] = 1;
                Q.push(v);
            }
            if(dis[v] == dis[u] + 1 && dp[u] + c >= dp[v]) {
                pre[v] = u;
                dp[v] = dp[u] + c;
            }
        }
    }
    vector<int > ans;
    int v = n;
    pre[1] = -1;
    while(pre[v] != -1) {
        M[make_pair(v, pre[v])] = 1;
        v = pre[v];
    }
    int len = 0;
    for(int i = 0; i < m; i++) {
        if(M[make_pair(x[i], y[i])] == 1 || M[make_pair(y[i], x[i])] == 1 && c[i] == 0) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 1;
        } else if(M[make_pair(x[i], y[i])] == 0 && M[make_pair(y[i], x[i])] == 0 && c[i] == 1) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 0;
        }
    }
    printf("%d\n",len);
    for(int i=0;i<len;i++)
        printf("%d %d %d\n",ax[i],ay[i],ac[i]);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int INF = 0x7fffffff;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
struct edge {
    int v, c;
    edge() {}
    edge(int v, int c): v(v), c(c) {}
};
map<pair<int, int>, int> M;
vector<edge> G[N];
queue<int> Q;
int n, m, x[N], y[N], c[N];
int dis[N], dp[N], pre[N];
int ax[N], ay[N], ac[N];
bool vis[N];

int main() {
#ifdef Tally_Ho
    freopen("in.txt", "r", stdin);
#endif // Tally_Ho
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d", &x[i], &y[i], &c[i]);
        G[x[i]].push_back(edge(y[i], c[i]));
        G[y[i]].push_back(edge(x[i], c[i]));
    }
    memset(pre,-1,sizeof(pre));
    Q.push(1);
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].v;
            int c = G[u][i].c; //1:working
            if(!vis[v]) {
                dis[v] = dis[u] + 1;
                vis[v] = 1;
                Q.push(v);
            }
            if(dis[v] == dis[u] + 1 && dp[u] + c >= dp[v]) {
                pre[v] = u;
                dp[v] = dp[u] + c;
            }
        }
    }
    vector<int > ans;
    int v = n;
    while(v != 1) {
        M[make_pair(v, pre[v])] = 1;
        v = pre[v];

    }
    int len = 0;
    for(int i = 0; i < m; i++) {
        if((M[make_pair(x[i], y[i])] == 1 || M[make_pair(y[i], x[i])] == 1) && c[i] == 0) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 1;
        } else if(M[make_pair(x[i], y[i])] == 0 && M[make_pair(y[i], x[i])] == 0 && c[i] == 1) {
            ax[len] = x[i], ay[len] = y[i], ac[len++] = 0;
        }
    }
    printf("%d\n",len);
    for(int i=0;i<len;i++)
        printf("%d %d %d\n",ax[i],ay[i],ac[i]);
}


/* Author : RISHAV GOYAL */

#include <bits/stdc++.h>

using namespace std;

#define LL long long int
#define UL unsigned long long int

#define imax INT_MAX
#define imin INT_MIN
#define LLmax LLONG_MAX
#define LLmin LLONG_MIN

#define FOR(i,a,b) for(int i= (int)a; i<= (int)b; i++)
#define rep(i,n) for(int i= int(1); i<= (int)n; i++)
#define FORd(i,a,b,d) for(int i=(int)a; i<= (int)b; i+=(int)d)

#define pr() printf("Reached here 1...\n");
#define pr1() printf("Reached here 2...\n");

#define CLR(a) memset(a,0,sizeof(a));
#define SET(a) memset(a,-1,sizeof(a));

#define str strlen
#define pb(x) push_back(x)
#define mp make_pair
#define ii pair<int,int>
#define ll pair<LL,LL>
#define F first
#define S second
#define gcd(a,b) __gcd(a,b)

#define mod int(1e9 +7)
#define MAX int(1e5 + 10)

#define si(a) scanf("%d",&a);

#define VI vector<int>
#define VL vector<LL>
#define VS vector<string>
#define VC vector<char>

LL powmod(LL a,int b,int n){LL rm=1;while (b){if (b % 2) { rm = (rm * a) % n; }a = (a * a) % n;b /= 2;}return rm;}

std::vector<ii> edge[MAX];
int n,m,sz[MAX]={0},it,f,prv,dp[MAX],Q[MAX],st[MAX]={0},working=0,x,y,z,tr[MAX];
std::map<ii, int> Hash;

void go(int id){
	if(id == 1) return ;
	Hash[mp(tr[id],id)] = Hash[mp(id,tr[id])] = 1;
	go(tr[id]);
}

int main()
{
	cin >> n >> m;

	rep(i,m){
		cin >>x >> y >> z;
		edge[x].pb(mp(y,z));
		edge[y].pb(mp(x,z));
		sz[x]++; sz[y]++;
		if(z) working++;
	} // not working in path + working - workingonpath.

	rep(i,n) dp[i] = 1e9;

	Q[++f] = 1;
	dp[1] = working;
	it = f = 1;
	int vrr;
	st[1] = 1;

	while(1){
		prv = f;
		for(int i=it;i<=prv;++i){
			vrr = Q[i];
			for(int j=0;j<sz[vrr];++j){
				if(st[edge[vrr][j].F] != 1){

				if(dp[edge[vrr][j].F] > dp[vrr] - (edge[vrr][j].S==1) + (edge[vrr][j].S==0)) tr[edge[vrr][j].F] = vrr;

					dp[edge[vrr][j].F] = min(dp[edge[vrr][j].F], dp[vrr] - (edge[vrr][j].S==1) + (edge[vrr][j].S==0) );
					if(!st[edge[vrr][j].F]) {
						Q[++f] = edge[vrr][j].F;

					}
					st[edge[vrr][j].F] = 2;
				}
			}
			for(int i1=prv+1;i1<=f;++i1) st[Q[i1]] = 1;
		}
		it = prv+1;
		if(st[n] ) break;
	}
	cout << dp[n] << endl;
	go(n);
	for(int i=1;i<=n;++i){
		for(int j=0;j<sz[i];++j){
			if(edge[i][j].F > i){
				if(Hash[mp(edge[i][j].F,i)] == 1){
					if(edge[i][j].S == 0){ // repair
						printf("%d %d 1\n", i,edge[i][j].F);
					}
				}
				else{
					if(edge[i][j].S == 1){
						printf("%d %d 0\n", i,edge[i][j].F);
					}
				}
			}
		}
	}
	return 0;
}


/* Author : RISHAV GOYAL */

#include <bits/stdc++.h>

using namespace std;

#define LL long long int
#define UL unsigned long long int

#define imax INT_MAX
#define imin INT_MIN
#define LLmax LLONG_MAX
#define LLmin LLONG_MIN

#define FOR(i,a,b) for(int i= (int)a; i<= (int)b; i++)
#define rep(i,n) for(int i= int(1); i<= (int)n; i++)
#define FORd(i,a,b,d) for(int i=(int)a; i<= (int)b; i+=(int)d)

#define pr() printf("Reached here 1...\n");
#define pr1() printf("Reached here 2...\n");

#define CLR(a) memset(a,0,sizeof(a));
#define SET(a) memset(a,-1,sizeof(a));

#define str strlen
#define pb(x) push_back(x)
#define mp make_pair
#define ii pair<int,int>
#define ll pair<LL,LL>
#define F first
#define S second
#define gcd(a,b) __gcd(a,b)

#define mod int(1e9 +7)
#define MAX int(1e5 + 10)

#define si(a) scanf("%d",&a);

#define VI vector<int>
#define VL vector<LL>
#define VS vector<string>
#define VC vector<char>

LL powmod(LL a,int b,int n){LL rm=1;while (b){if (b % 2) { rm = (rm * a) % n; }a = (a * a) % n;b /= 2;}return rm;}

std::vector<ii> edge[MAX];
int n,m,sz[MAX]={0},it,f,prv,dp[MAX],Q[MAX],st[MAX]={0},working=0,x,y,z,tr[MAX];
std::map<ii, int> Hash;

void go(int id){
	if(id == 1) return ;
	Hash[mp(tr[id],id)] = Hash[mp(id,tr[id])] = 1;
	go(tr[id]);
}

int main()
{
	cin >> n >> m;

	rep(i,m){
		cin >>x >> y >> z;
		edge[x].pb(mp(y,z));
		edge[y].pb(mp(x,z));
		sz[x]++; sz[y]++;
		if(z) working++;
	} // not working in path + working - workingonpath.

	rep(i,n) dp[i] = 1e9;

	Q[++f] = 1;
	dp[1] = working;
	it = f = 1;
	int vrr;
	st[1] = 1;

	while(1){
		prv = f;

		for(int i=it;i<=prv;++i){
			vrr = Q[i];

			for(int j=0;j<sz[vrr];++j){

				if(st[edge[vrr][j].F] != 1){

				if(dp[edge[vrr][j].F] > dp[vrr] - (edge[vrr][j].S==1) + (edge[vrr][j].S==0)) tr[edge[vrr][j].F] = vrr;

					dp[edge[vrr][j].F] = min(dp[edge[vrr][j].F], dp[vrr] - (edge[vrr][j].S==1) + (edge[vrr][j].S==0) );

					if(!st[edge[vrr][j].F]) {
						Q[++f] = edge[vrr][j].F;
					}
					st[edge[vrr][j].F] = 2;
				}
			}

		}
		for(int i1=prv+1;i1<=f;++i1) st[Q[i1]] = 1;
		it = prv+1;
		if(st[n] ) break;
	}
	cout << dp[n] << endl;
	go(n);
	for(int i=1;i<=n;++i){
		for(int j=0;j<sz[i];++j){
			if(edge[i][j].F > i){
				if(Hash[mp(edge[i][j].F,i)] == 1){
					if(edge[i][j].S == 0){ // repair
						printf("%d %d 1\n", i,edge[i][j].F);
					}
				}
				else{
					if(edge[i][j].S == 1){
						printf("%d %d 0\n", i,edge[i][j].F);
					}
				}
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

struct state{
    int u,c,r;
    state(int u,int c,int r):u(u),c(c),r(r){}
    bool operator<(const state&e)const{
        if(e.c!=c)
            return e.c<c;
        return e.r<r;
    }
};

const int N=1e5;
vector<pair<int,int> >g[N];
int n,m;
pair<int,int>cost[N];
int par[N];
set<pair<int,int> >bad;
vector<state>res;
vector<pair<int,int> >all,good;
set<pair<int,int> >::iterator it;
bool vis[N];

void print(){
    for(int u=n-1;u;u=par[u]){
        int v=par[u];
        if(bad.find(make_pair(min(u,v),max(u,v)))!=bad.end())
            res.push_back(state(u,v,1));
        else
            all.push_back(make_pair(min(u,v),max(u,v)));
    }
    sort(all.begin(),all.end());
    for(size_t a=0;a<good.size();++a)
        if(!binary_search(all.begin(),all.end(),good[a]))
            res.push_back(state(good[a].first,good[a].second,0));
    cout<<res.size()<<"\n";
    for(size_t a=0;a<res.size();++a)
        cout<<res[a].u+1<<" "<<res[a].c+1<<" "<<res[a].r<<"\n";
    exit(0);
}

void dijkstra(){
    priority_queue<state>qu;
    qu.push(state(0,0,0));
    for(int a=0;a<n;++a)
        cost[a]=make_pair(2e9,2e9);
    cost[0]=make_pair(0,0);
    par[0]=-1;
    while(qu.size()){
        state e=qu.top();
        qu.pop();
        if(vis[e.u])
            continue;
        vis[e.u]=1;
        if(e.u==n-1)
            print();
        for(size_t a=0;a<g[e.u].size();++a){
            int u=g[e.u][a].first;
            int c=e.c+1;
            int r=e.r+g[e.u][a].second;
            if(vis[u]||make_pair(c,r)>=cost[u])
                continue;
            cost[u]=make_pair(c,r);
            par[u]=e.u;
            qu.push(state(u,c,r));
        }
    }
}

int main(int argc, char **argv) {
    scanf("%d%d",&n,&m);
    for(int a=0;a<m;++a){
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        --u;--v;
        g[u].push_back(make_pair(v,!c));
        g[v].push_back(make_pair(u,!c));
        if(c)
            good.push_back(make_pair(min(u,v),max(u,v)));
        else
            bad.insert(make_pair(min(u,v),max(u,v)));
    }
    dijkstra();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

struct state{
    int u,c,r;
    state(int u,int c,int r):u(u),c(c),r(r){}
    bool operator<(const state&e)const{
        if(e.c!=c)
            return e.c<c;
        return e.r<r;
    }
};

const int N=1e5;
vector<pair<int,int> >g[N];
int n,m;
pair<int,int>cost[N];
int par[N];
set<pair<int,int> >bad;
vector<state>res;
vector<pair<int,int> >all,good;
set<pair<int,int> >::iterator it;

void print(){
    for(int u=n-1;u;u=par[u]){
        int v=par[u];
        if(bad.find(make_pair(min(u,v),max(u,v)))!=bad.end())
            res.push_back(state(u,v,1));
        else
            all.push_back(make_pair(min(u,v),max(u,v)));
    }
    sort(all.begin(),all.end());
    for(size_t a=0;a<good.size();++a)
        if(!binary_search(all.begin(),all.end(),good[a]))
            res.push_back(state(good[a].first,good[a].second,0));
    cout<<res.size()<<"\n";
    for(size_t a=0;a<res.size();++a)
        cout<<res[a].u+1<<" "<<res[a].c+1<<" "<<res[a].r<<"\n";
    exit(0);
}

void dijkstra(){
    priority_queue<state>qu;
    qu.push(state(0,0,0));
    for(int a=0;a<n;++a)
        cost[a]=make_pair(2e9,2e9);
    cost[0]=make_pair(0,0);
    par[0]=-1;
    while(qu.size()){
        state e=qu.top();
        qu.pop();
        if(e.u==n-1)
            print();
        for(size_t a=0;a<g[e.u].size();++a){
            int u=g[e.u][a].first;
            int c=e.c+1;
            int r=e.r+g[e.u][a].second;
            if(make_pair(c,r)>=cost[u])
                continue;
            cost[u]=make_pair(c,r);
            par[u]=e.u;
            qu.push(state(u,c,r));
        }
    }
}

int main(int argc, char **argv) {
    scanf("%d%d",&n,&m);
    for(int a=0;a<m;++a){
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        --u;--v;
        g[u].push_back(make_pair(v,!c));
        g[v].push_back(make_pair(u,!c));
        if(c)
            good.push_back(make_pair(min(u,v),max(u,v)));
        else
            bad.insert(make_pair(min(u,v),max(u,v)));
    }
    dijkstra();
    return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
#include<map>
using namespace std;
const int N=(int)1e+5;
int n,m,bl,step,x[N],y[N],c[N],vis[N],dis[N],dp[N],app[N],path[N];
vector< pair<int,int> > G[N];
struct e{
    int v,w,st;
};
queue<int> q;
void bfs(){
    q.push(1);
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=0;i<(int)G[u].size();i++){
            int v=G[u][i].first;
            int c=G[u][i].second;
            if(!vis[v]){
                dis[v]=dis[u]+1;
                vis[v]=1;
                q.push(v);
            }
            if(dis[v]==dis[u]+1 && dp[u]+c>=dp[v]){
                path[v]=u;
                dp[v]=dp[u]+c;
            }
        }
    }
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",x+i,y+i,c+i);
        G[x[i]].push_back(make_pair(y[i],c[i]));
        G[y[i]].push_back(make_pair(x[i],c[i]));
        bl+=c[i];
    }
    bfs();
    cout<<dis[n]-2*dp[n]+bl<<endl;
    int v=n;
    while(v!=1){
        app[v]=1;
        v=path[v];
    }app[1]=1;
    /*for(int i=1;i<=n;i++)
        cout<<app[i]<<" ";cout<<endl;
    for(int i=1;i<=n;i++)
        cout<<path[i]<<" ";cout<<endl;*/
    for(int i=1;i<=m;i++){
        if(!(app[x[i]] && app[y[i]]) && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]);
        else if((path[y[i]]==x[i] || path[x[i]]==y[i]) && c[i]==0 && app[x[i]] && app[y[i]])
            printf("%d %d 1\n",x[i],y[i]);
    }
    return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
const int N=(int)1e+5;
int n,m,bl,step,x[N],y[N],c[N],vis[N],ans,res,path[N],app[N],p[N];
vector< pair<int,int> > G[N];
struct e{
    int v,w,st;
};
queue<e> q;

void bfs(){
    while(!q.empty()) q.pop();
    e t;t.v=1;t.st=0;t.w=0;
    q.push(t);
    ans=99999999;
    res=99999999;
    vis[1]=1;
    while(!q.empty()){
        t=q.front();q.pop();
        if(ans<t.st) continue;

        for(int i=0;(int)i<G[t.v].size();i++) {
            int u=G[t.v][i].first;
                e neo=t;
                if(u!=n && vis[u]) continue;
                vis[u]=1;
                path[u]=t.v;
                neo.v=u;
                neo.st++;
                if(!G[t.v][i].second) neo.w++;
                q.push(neo);
                if(neo.v==n){
                    if(ans>neo.st) ans=neo.st;
                    if(ans==neo.st){
                        int d=bl+2*neo.w-neo.st;
                        if(res>d){
                            res=d;
                            //printf("%d %d %d %d\n",d,bl,t.w,t.st);
                            for(int i=1;i<=n;i++){
                                p[i]=path[i];
                                //cout<<p[i]<<" ";
                            }      // cout<<endl;
                        }
                    }
                }
        }

    }
}

void lu(int i){
    if(i==1) return;
    lu(p[i]);
    //cout<<i<<" ";
    app[i]=1;
    return;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",x+i,y+i,c+i);
        G[x[i]].push_back(make_pair(y[i],c[i]));
        G[y[i]].push_back(make_pair(x[i],c[i]));
        if(c[i]) bl++;
    }
    bfs();
    cout<<res<<endl;
    lu(n);//cout<<endl;
    app[1]=1;
    for(int i=1;i<=m;i++){
        if(!(app[x[i]] && app[y[i]]) && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]);
        else if((p[y[i]]==x[i] || p[x[i]]==y[i]) && c[i]==0 && app[x[i]] && app[y[i]])
            printf("%d %d 1\n",x[i],y[i]);
    }
    return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
const int N=(int)1e+5;
int n,m,bl,step,x[N],y[N],c[N],vis[N],ans,res,path[N],shi[N],p[N];
vector< pair<int,int> > G[N];
struct e{
    int v,w,st;
};
queue<e> q;

void bfs(){
    while(!q.empty()) q.pop();
    e t;t.v=1;t.st=0;t.w=0;
    q.push(t);
    ans=99999999;
    res=99999999;
    vis[1]=1;
    while(!q.empty()){
        t=q.front();q.pop();
        if(ans<t.st) continue;

        for(int i=0;(int)i<G[t.v].size();i++) {
            int u=G[t.v][i].first;
                e neo=t;
                if(u!=n && vis[u]) continue;
                vis[u]=1;
                path[u]=t.v;
                neo.v=u;
                neo.st++;
                if(!G[t.v][i].second) neo.w++;
                q.push(neo);
                if(neo.v==n){
                    if(ans>neo.st) ans=neo.st;
                    if(ans==neo.st){
                        int d=bl+2*neo.w-neo.st;
                        if(res>d){
                            res=d;
                            //printf("%d %d %d %d\n",d,bl,t.w,t.st);
                            for(int i=1;i<=n;i++){
                                p[i]=path[i];
                                //cout<<p[i]<<" ";
                            }      // cout<<endl;
                        }
                    }
                }
        }

    }
}

void lu(int i){
    if(i==0) return;
    lu(p[i]);
    //cout<<i<<" ";
    shi[i]=1;
    if(i==n) return;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",x+i,y+i,c+i);
        G[x[i]].push_back(make_pair(y[i],c[i]));
        G[y[i]].push_back(make_pair(x[i],c[i]));
        if(c[i]) bl++;
    }
    bfs();
    cout<<res<<endl;
    lu(n);//cout<<endl;

    for(int i=1;i<=m && res>0;i++){
        if(shi[y[i]] && p[y[i]]==x[i] && c[i]==0)
            printf("%d %d 1\n",x[i],y[i]),res--;
        else if(!shi[y[i]] && p[y[i]]==x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]),res--;
        else if((p[y[i]]!=x[i] && p[x[i]]!=y[i]) && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]),res--;
    }
    return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
const int N=(int)1e+5;
int n,m,bl,step,x[N],y[N],c[N],vis[N],ans,res,path[N],shi[N],p[N];
vector< pair<int,int> > G[N];
struct e{
    int v,w,st;
};
queue<e> q;

void bfs(){
    while(!q.empty()) q.pop();
    e t;t.v=1;t.st=0;t.w=0;
    q.push(t);
    ans=99999999;
    res=99999999;
    vis[1]=1;
    while(!q.empty()){
        t=q.front();q.pop();
        if(ans<t.st) continue;

        for(int i=0;(int)i<G[t.v].size();i++) {
            int u=G[t.v][i].first;
                e neo=t;
                if(u!=n && vis[u]) continue;
                vis[u]=1;
                path[u]=t.v;
                neo.v=u;
                neo.st++;
                if(!G[t.v][i].second) neo.w++;
                q.push(neo);
                if(neo.v==n){
                    if(ans>neo.st) ans=neo.st;
                    if(ans==neo.st){
                        int d=bl+2*neo.w-neo.st;
                        if(res>d){
                            res=d;
                            //printf("%d %d %d %d\n",d,bl,t.w,t.st);
                            for(int i=1;i<=n;i++){
                                p[i]=path[i];
                                //cout<<p[i]<<" ";
                            }      // cout<<endl;
                        }
                    }
                }
        }

    }
}

void lu(int i){
    if(i==0) return;
    lu(p[i]);
    //cout<<i<<" ";
    shi[i]=1;
    if(i==n) return;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",x+i,y+i,c+i);
        G[x[i]].push_back(make_pair(y[i],c[i]));
        G[y[i]].push_back(make_pair(x[i],c[i]));
        if(c[i]) bl++;
    }
    bfs();
    cout<<res<<endl;
    lu(n);//cout<<endl;

    for(int i=1;i<=m;i++){
        if(shi[y[i]] && p[y[i]]==x[i] && c[i]==0)
            printf("%d %d 1\n",x[i],y[i]);
        if(!shi[y[i]] && p[y[i]]==x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]);
        if(p[y[i]]!=x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]);
    }
    system("pause");
}


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
const int N=(int)1e+5;
int n,m,bl,step,x[N],y[N],c[N],vis[N],ans,res,path[N],shi[N],p[N];
vector< pair<int,int> > G[N];
struct e{
    int v,w,st;
};
queue<e> q;

void bfs(){
    while(!q.empty()) q.pop();
    e t;t.v=1;t.st=0;t.w=0;
    q.push(t);
    ans=99999999;
    res=99999999;
    vis[1]=1;
    while(!q.empty()){
        t=q.front();q.pop();
        if(ans<t.st) continue;

        for(int i=0;(int)i<G[t.v].size();i++) {
            int u=G[t.v][i].first;
                e neo=t;
                if(u!=n && vis[u]) continue;
                vis[u]=1;
                path[u]=t.v;
                neo.v=u;
                neo.st++;
                if(!G[t.v][i].second) neo.w++;
                q.push(neo);
                if(neo.v==n){
                    if(ans>neo.st) ans=neo.st;
                    if(ans==neo.st){
                        int d=bl+2*neo.w-neo.st;
                        if(res>d){
                            res=d;
                            //printf("%d %d %d %d\n",d,bl,t.w,t.st);
                            for(int i=1;i<=n;i++){
                                p[i]=path[i];
                                //cout<<p[i]<<" ";
                            }      // cout<<endl;
                        }
                    }
                }
        }

    }
}

void lu(int i){
    if(i==0) return;
    lu(p[i]);
    //cout<<i<<" ";
    shi[i]=1;
    if(i==n) return;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",x+i,y+i,c+i);
        G[x[i]].push_back(make_pair(y[i],c[i]));
        G[y[i]].push_back(make_pair(x[i],c[i]));
        if(c[i]) bl++;
    }
    bfs();
    cout<<res<<endl;
    lu(n);//cout<<endl;

    for(int i=1;i<=m;i++){
        if(shi[y[i]] && p[y[i]]==x[i] && c[i]==0)
            printf("%d %d 1\n",x[i],y[i]);
        if(!shi[y[i]] && p[y[i]]==x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]);
        if(p[y[i]]!=x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]);
    }
    return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
const int N=(int)1e+5;
int n,m,bl,step,x[N],y[N],c[N],vis[N],ans,res,path[N],shi[N],p[N];
vector< pair<int,int> > G[N];
struct e{
    int v,w,st;
};
queue<e> q;

void bfs(){
    while(!q.empty()) q.pop();
    e t;t.v=1;t.st=0;t.w=0;
    q.push(t);
    ans=99999999;
    res=99999999;
    vis[1]=1;
    while(!q.empty()){
        t=q.front();q.pop();
        if(ans<t.st) continue;

        for(int i=0;(int)i<G[t.v].size();i++) {
            int u=G[t.v][i].first;
                e neo=t;
                if(u!=n && vis[u]) continue;
                vis[u]=1;
                path[u]=t.v;
                neo.v=u;
                neo.st++;
                if(!G[t.v][i].second) neo.w++;
                q.push(neo);
                if(neo.v==n){
                    if(ans>neo.st) ans=neo.st;
                    if(ans==neo.st){
                        int d=bl+2*neo.w-neo.st;
                        if(res>d){
                            res=d;
                            //printf("%d %d %d %d\n",d,bl,t.w,t.st);
                            for(int i=1;i<=n;i++){
                                p[i]=path[i];
                                //cout<<p[i]<<" ";
                            }      // cout<<endl;
                        }
                    }
                }
        }

    }
}

void lu(int i){
    if(i==0) return;
    lu(p[i]);
    //cout<<i<<" ";
    shi[i]=1;
    if(i==n) return;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",x+i,y+i,c+i);
        G[x[i]].push_back(make_pair(y[i],c[i]));
        G[y[i]].push_back(make_pair(x[i],c[i]));
        if(c[i]) bl++;
    }
    bfs();
    cout<<res<<endl;
    lu(n);//cout<<endl;

    for(int i=1;i<=m && res>0;i++){
        //if(!shi[y[i]]) continue;
        if(shi[y[i]] && p[y[i]]==x[i] && c[i]==0)
            printf("%d %d 1\n",x[i],y[i]),res--;
        else if(!shi[y[i]] && p[y[i]]==x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]),res--;
        else if(p[y[i]]!=x[i] && c[i]==1)
            printf("%d %d 0\n",x[i],y[i]),res--;
    }
    return 0;
}


// Template {{{
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;

template<typename T1, typename T2>
ostream& operator << (ostream& os, const pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template<typename T>
ostream& operator << (ostream& os, const vector<T>& v) {
    os << "{";
    for(int i = 0; i < v.size(); i++)
        os << v[i] << (i+1<v.size()?", ":"");
    os << "}";
    return os;
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
//}}}

const int INF = 1000000000;
struct Edge{
    int src, dst, cost, id;
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
vector<int> dijkstra(const Graph& G, int s){
    typedef pair<int, int> P;
    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> dist(G.size(), INF);
    que.push(P(0, s));
    dist[s] = 0;
    while(!que.empty()){
        P p = que.top(); que.pop();
        int v = p.second, c = p.first;
        if(c > dist[v]) continue;
        for(auto& e : G[v]){
            if(dist[e.dst] > dist[v] + e.cost){
                dist[e.dst] = dist[v] + e.cost;
                que.push(P(dist[e.dst], e.dst));
            }
        }
    }
    return dist;
}

int main(){
    iostream_init();

    int N, M;
    while(cin >> N >> M) {
        Graph G(N);
        vector<int> A(M), B(M), C(M);
        REP(i, M) {
            cin >> A[i] >> B[i] >> C[i];
            const int u = A[i]-1, v = B[i]-1, c = C[i];
            G[u].push_back({u, v, c, i});
            G[v].push_back({v, u, c, i});
        }
        vector<int> dist = dijkstra(G, 0);
        vector<int> dp(N, -INF);
        dp[N-1] = 0;
        vector<pair<int, int>> ps(N);
        REP(i, N) ps[i] = make_pair(dist[i], i);
        sort(ps.begin(), ps.end());
        reverse(ps.begin(), ps.end());
        vector<int> eid(N, -1);
        vector<int> to(N, -1);
        for(const auto& p : ps) {
            int u = p.second;
            for(Edge e : G[u]) if(dp[e.dst] >= 0) {
                if(dp[u] < dp[e.dst] + e.cost) {
                    dp[u] = dp[e.dst] + e.cost;
                    eid[u] = e.id;
                    to[u] = e.dst;
                }
            }
        }
        vector<bool> used(M);
        for(int cur = 0; cur != N-1;) {
            int e = eid[cur];
            assert(e != -1);
            used[ e ] = true;
            assert(cur != to[cur]);
            cur = to[cur];
        }

        vector<tuple<int, int, int>> ans;
        REP(i, M) {
            if(used[i] && C[i] == 0) {
                ans.push_back(make_tuple(A[i], B[i], 1));
            } else if(!used[i] && C[i] == 1) {
                ans.push_back(make_tuple(A[i], B[i], 0));
            }
        }
        cout << ans.size() << endl;
        REP(i, ans.size()) {
            int a, b, c;
            tie(a, b, c) = ans[i];
            cout << a << " " << b << " " << c << endl;
        }
    }

    return 0;
}

/* vim:set foldmethod=marker: */


// Template {{{
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;

template<typename T1, typename T2>
ostream& operator << (ostream& os, const pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template<typename T>
ostream& operator << (ostream& os, const vector<T>& v) {
    os << "{";
    for(int i = 0; i < v.size(); i++)
        os << v[i] << (i+1<v.size()?", ":"");
    os << "}";
    return os;
}

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
//}}}

const int INF = 1000000000;
struct Edge{
    int src, dst, cost, id;
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
vector<int> dijkstra(const Graph& G, int s){
    typedef pair<int, int> P;
    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> dist(G.size(), INF);
    que.push(P(0, s));
    dist[s] = 0;
    while(!que.empty()){
        P p = que.top(); que.pop();
        int v = p.second, c = p.first;
        if(c > dist[v]) continue;
        for(auto& e : G[v]){
            if(dist[e.dst] > dist[v] + 1){
                dist[e.dst] = dist[v] + 1;
                que.push(P(dist[e.dst], e.dst));
            }
        }
    }
    return dist;
}

int main(){
    iostream_init();

    int N, M;
    while(cin >> N >> M) {
        Graph G(N);
        vector<int> A(M), B(M), C(M);
        REP(i, M) {
            cin >> A[i] >> B[i] >> C[i];
            const int u = A[i]-1, v = B[i]-1, c = C[i];
            G[u].push_back({u, v, c, i});
            G[v].push_back({v, u, c, i});
        }
        vector<int> dist = dijkstra(G, 0);
        vector<int> dp(N, -INF);
        dp[N-1] = 0;
        vector<pair<int, int>> ps(N);
        REP(i, N) ps[i] = make_pair(dist[i], i);
        sort(ps.begin(), ps.end());
        reverse(ps.begin(), ps.end());
        vector<int> eid(N, -1);
        vector<int> to(N, -1);
        for(const auto& p : ps) {
            int u = p.second;
            for(Edge e : G[u]) if(dp[e.dst] >= 0) {
                if(dp[u] < dp[e.dst] + e.cost && dist[u] + 1 == dist[e.dst]) {
                    dp[u] = dp[e.dst] + e.cost;
                    eid[u] = e.id;
                    to[u] = e.dst;
                }
            }
        }
        vector<bool> used(M);
        for(int cur = 0; cur != N-1;) {
            int e = eid[cur];
            assert(e != -1);
            used[ e ] = true;
            assert(cur != to[cur]);
            cur = to[cur];
        }

        vector<tuple<int, int, int>> ans;
        REP(i, M) {
            if(used[i] && C[i] == 0) {
                ans.push_back(make_tuple(A[i], B[i], 1));
            } else if(!used[i] && C[i] == 1) {
                ans.push_back(make_tuple(A[i], B[i], 0));
            }
        }
        cout << ans.size() << endl;
        REP(i, ans.size()) {
            int a, b, c;
            tie(a, b, c) = ans[i];
            cout << a << " " << b << " " << c << endl;
        }
    }

    return 0;
}

/* vim:set foldmethod=marker: */


#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <limits>
#include <set>
#include <cmath>
#include <cstring>

using namespace std;
#define pb push_back
#define MAXN 100100

struct edge_t
{
    int x,y;
    bool usato,z;
};

edge_t edge[MAXN];
vector<int> adj[MAXN];
int dist[MAXN],usate[MAXN];
int N,M,a,b,c;
queue<int> Q;
vector<tuple<int,int,int> > sol;

int main()
{
    scanf("%d%d",&N,&M);
	for(int i=0;i<M;++i)
    {
        scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
        adj[edge[i].x].pb(i);
        adj[edge[i].y].pb(i);
    }
    memset(dist,-1,sizeof dist);
    Q.push(1);
    dist[1]=0;
    while(!Q.empty())
    {
        int node=Q.front(),dest;
        Q.pop();
        for(auto e:adj[node])
        {
            dest=(edge[e].x==node) ? edge[e].y : edge[e].x;
            if(dist[dest]>dist[node]+1 || dist[dest]==-1)
            {
                dist[dest]=dist[node]+1;
                usate[dest]=usate[node]+(edge[e].z ? 1:0);
                Q.push(dest);
            }
            else if(dist[dest]==dist[node]+1)
            {
                usate[dest]=max(usate[dest],usate[node]+(edge[e].z ? 1:0));
            }
        }
    }

    int node=N,dest;
    while(node!=1)
    {
        //cerr<<node<<" "<<dist[node]<<usate[node]<<endl;
        for(auto e:adj[node])
        {
            dest=(edge[e].x==node) ? edge[e].y : edge[e].x;
            if(dist[dest]+1==dist[node] && usate[node]==usate[dest]+(edge[e].z ? 1:0))
            {
                edge[e].usato=true;
                node=dest; break;
            }
        }
    }

    for(int i=0;i<M;++i)
    {
        if((edge[i].usato && !edge[i].z) || (!edge[i].usato && edge[i].z)) sol.pb(make_tuple(edge[i].x,edge[i].y,!edge[i].z));
    }
    printf("%d\n",sol.size());
    for(int i=0;i<sol.size();++i)
    {
        tie(a,b,c)=sol[i];
        printf("%d %d %d\n",a,b,c);
    }
    return 0;

}


#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <limits>
#include <set>
#include <cmath>
#include <cstring>

using namespace std;
#define pb push_back
#define MAXN 100100

struct edge_t
{
    int x,y;
    bool usato,z;
};

edge_t edge[MAXN];
vector<int> adj[MAXN];
int dist[MAXN],usate[MAXN];
int N,M,a,b,c;
queue<int> Q;
vector<tuple<int,int,int> > sol;

int main()
{
    scanf("%d%d",&N,&M);
	for(int i=0;i<M;++i)
    {
        scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
        adj[edge[i].x].pb(i);
        adj[edge[i].y].pb(i);
    }
    memset(dist,-1,sizeof dist);
    Q.push(1);
    dist[1]=0;
    while(!Q.empty())
    {
        int node=Q.front(),dest;
        Q.pop();
        for(auto e:adj[node])
        {
            dest=(edge[e].x==node) ? edge[e].y : edge[e].x;
            if(dist[dest]>dist[node]+1 || dist[dest]==-1)
            {
                dist[dest]=dist[node]+1;
                usate[dest]=usate[node]+(edge[e].z ? 1:0);
                Q.push(dest);
            }
            else if(dist[dest]==dist[node]+1)
            {
                usate[dest]=max(usate[dest],usate[node]+(edge[e].z ? 1:0));
            }
        }
    }

    int node=N,dest;
    while(node!=1)
    {
        //cerr<<node<<" "<<dist[node]<<usate[node]<<endl;
        for(auto e:adj[node])
        {
            dest=(edge[e].x==node) ? edge[e].y : edge[e].x;
            if(dist[dest]+1==dist[node] && usate[node]==usate[dest]+(edge[e].z ? 1:0))
            {
                edge[e].usato=true;
                node=dest; break;
            }
        }
    }

    for(int i=0;i<M;++i)
    {
        if((edge[i].usato && !edge[i].z) || (!edge[i].usato && edge[i].z)) sol.pb(make_tuple(edge[i].x,edge[i].y,!edge[i].z));
    }
    printf("%d\n",sol.size());
    for(int i=0;i<sol.size();++i)
    {
        tie(a,b,c)=sol[i];
        printf("%d %d %d\n",a,b,c);
    }
    return 0;

}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=3e5+5;
const ll INF=1e18;
const ll mod=998244353;
const double PI = 3.14159265358979323846;

#define is(n) scanf("%d",&n)
#define io(n) printf("%d\n",n)
#define ls(n) scanf("%lld",&n)
#define lo(n) printf("%lld\n",n)

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

queue<ll> f;
vector<ll> g[N];
ll n,m,u,v,vis[N];

void BFS()
{
  f.pop();
  queue<ll> q;
  q.push(1);
  vis[1]=1;
  while(q.size())
  {
    vector<ll> temp,rev,store;
    ll cur=q.front();q.pop();
    for(auto i:g[cur])
    {
      if(vis[i]){continue;}
      vis[i]=1;
      temp.pb(i);
    }
    sort(all(temp));
    ll cnt=temp.size(),check=0;
    while(f.size()&&check<cnt)
    {
      check++;
      rev.pb(f.front());
      store.pb(f.front());
      f.pop();
    }
    sort(all(rev));
    if(check!=cnt||rev!=temp){cout<<"No"<<endl;exit(0);}
    for(auto i:store){q.push(i);}
  }
}

int main()
{
  cin>>n;
  m=n-1;
  while(m--)
  {
    cin>>u>>v;
    g[u].pb(v);
    g[v].pb(u);
  }
  for(ll i=0;i<n;i++){cin>>u;f.push(u);}
  if(f.front()!=1){cout<<"No"<<endl;exit(0);}
  BFS();
  cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=2e5+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N],vlev[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N],maxi;

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				maxi=max(maxi,lev[i]);
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(1);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);vlev[lev[dup[i]]].pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	/*for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[par[cp[cp.size()-1]]]){cout<<"No"<<endl;exit(0);}
	}*/
	for(ll i=1;i<maxi;i++)
	{
		ll cur=0;
		for(ll j=0;j<vlev[i+1].size()-1;j++)
		{
			if(cur>=lev[i].size()){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=par[vlev[i+1][j+1]]){cur++;}
		}
		if(vlev[i+1].size()==0){continue;}
		if(par[vlev[i+1][vlev[i+1].size()-1]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
	}
	cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=2e5+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N],vlev[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N],maxi;

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				maxi=max(maxi,lev[i]);
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(1);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);vlev[lev[dup[i]]].pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	/*for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[par[cp[cp.size()-1]]]){cout<<"No"<<endl;exit(0);}
	}*/
	for(ll i=1;i<maxi;i++)
	{
		ll cur=0;
		for(ll j=0;j<vlev[i+1].size()-1;j++)
		{
			if(cur>=vlev[i].size()){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=par[vlev[i+1][j+1]]){cur++;}
		}
		if(vlev[i+1].size()==0){continue;}
		if(par[vlev[i+1][vlev[i+1].size()-1]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
	}
	cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=2e5+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N],vlev[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N],maxi;

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				maxi=max(maxi,lev[i]);
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(1);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);vlev[lev[dup[i]]].pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	/*for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[par[cp[cp.size()-1]]]){cout<<"No"<<endl;exit(0);}
	}*/
	for(ll i=1;i<maxi;i++)
	{
		ll cur=0,ff=0;
		for(ll j=0;j<vlev[i+1].size()-1;j++)
		{
			ff++;
	//		if(lev[cur]==i){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=par[vlev[i+1][j+1]]){cur++;}
		}
		if(!ff){continue;}
		if(par[vlev[i+1][vlev[i+1].size()-1]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
	}
	cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=2e5+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N],vlev[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N],maxi;

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				maxi=max(maxi,lev[i]);
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(1);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);vlev[lev[dup[i]]].pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[par[cp[cp.size()-1]]]){cout<<"No"<<endl;exit(0);}
	}
	for(ll i=1;i<maxi;i++)
	{
		ll cur=0;
		for(ll j=0;j<vlev[i+1].size()-1;j++)
		{
			if(par[vlev[i+1][j]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=par[vlev[i+1][j+1]]){cur++;}
		}
		if(par[vlev[i+1][vlev[i+1].size()-1]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
	}
	cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=2e5+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N],vlev[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N],maxi;

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				maxi=max(maxi,lev[i]);
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(dup[0]);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);vlev[lev[dup[i]]].pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	/*for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[par[cp[cp.size()-1]]]){cout<<"No"<<endl;exit(0);}
	}*/
	for(ll i=1;i<maxi;i++)
	{
		ll cur=0;
		for(ll j=0;j<vlev[i+1].size()-1;j++)
		{
			if(par[vlev[i+1][j]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=par[vlev[i+1][j+1]]){cur++;}
		}
		if(par[vlev[i+1][vlev[i+1].size()-1]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
	}
	cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=1e6+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N];

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(1);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	//cout<<endl;
	//for(ll i=0;i<cp.size();i++){cout<<cp[i]<<" "<<par[cp[i]]<<endl;}
	//cout<<endl;
	for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[cp[cp.size()-1]]){cout<<"No"<<endl;exit(0);}
	}
	cout<<"Yes"<<endl;
}


// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg=22;
const ll N=2e5+5;
const ll INF=1e18;
const ll mod=1e9+7;
const double PI = 3.14159265358979323846;

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define OPT ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}

vector<ll> g[N],vlev[N];
ll n,m,u,v,vis[N],lev[N],par[N],done[N],maxi;

void BFS(ll cur)
{
	par[cur]=0;
	queue<ll> q;
	q.push(cur);
	vis[cur]=1;
	lev[cur]=1;
	while(q.size())
	{
		cur=q.front();q.pop();
		for(ll i:g[cur])
		{
			if(!vis[i])
			{
				vis[i]=1;
				par[i]=cur;
				lev[i]=lev[cur]+1;
				maxi=max(maxi,lev[i]);
				q.push(i);
			}
		}
	}
}

int main()
{
	cin>>n;
	m=n-1;
	vector<ll> dup;
	while(m--)
	{
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(ll i=0;i<n;i++){cin>>u;dup.pb(u);}
	BFS(1);
	vector<ll> ans,check,cp;
	for(ll i=0;i<n;i++){ans.pb(lev[dup[i]]);check.pb(lev[dup[i]]);cp.pb(dup[i]);vlev[lev[dup[i]]].pb(dup[i]);}
	sort(all(check));
	for(ll i=0;i<ans.size();i++)
	{
		if(ans[i]!=check[i]){cout<<"No"<<endl;exit(0);}
	}
	//cout<<endl;
	//for(ll i=0;i<cp.size();i++){cout<<cp[i]<<" "<<par[cp[i]]<<endl;}
	//cout<<endl;
	for(ll i=0;i<cp.size()-1;i++)
	{
		if(done[par[cp[i]]]){cout<<"No"<<endl;exit(0);}
		if(par[cp[i]]!=par[cp[i+1]]){done[par[cp[i]]]=1;}
	}
	if(cp.size()>=2)
	{
		if(cp[cp.size()-1]!=cp[cp.size()-2]&&done[par[cp[cp.size()-1]]]){cout<<"No"<<endl;exit(0);}
	}

	/*for(ll i=1;i<=maxi;i++)
	{
		cout<<i<<" : "<<endl;
		for(ll j:vlev[i]){cout<<j<<" ";}
		cout<<endl;
	}*/

	//cout<<"hi"<<endl;

	for(ll i=1;i<maxi;i++)
	{
		ll cur=0;
	//	cout<<vlev[i][cur]<<" : ";
		for(ll j=0;j<vlev[i+1].size()-1;j++)
		{
		//	cout<<vlev[i+1][cur]<<" ";
			if(par[vlev[i+1][j]]!=vlev[i][cur]){cout<<"No"<<endl;exit(0);}
			if(par[vlev[i+1][j]]!=par[vlev[i+1][j+1]]){cur++;}
		}
		//cout<<endl;
	}

	cout<<"Yes"<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	int pt[n + 10];
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		pt[y] = x;
	}
	int ar[n + 10];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	if(ar[0] != 1){
		cout << "NO" << endl;
		return 0;
	}
	while(curc < n && curp < curc){
		if(pt[ar[curc]] == ar[curp] || pt[ar[curp]] == ar[curc]){
			curc++;
		}else{
			curp++;
		}
	}
	if(curp < curc){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	int pt[n + 10];
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		pt[y] = x;
	}
	int ar[n + 10];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	while(curc < n && curp < curc){
		if(pt[ar[curc]] == ar[curp] || pt[ar[curp]] == ar[curc]){
			curc++;
		}else{
			curp++;
		}
	}
	if(curp < curc){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	set <set<int> >s;
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		s.insert({x , y});
	}
	int ar[n + 10];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	while(curc < n && curp < curc){
		if(s.count({ar[curp] , ar[curc]}) == 1){
			curc++;
		}else{
			curp++;
		}
	}
	if(curp < curc){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	unordered_map<int , int> pt;
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		pt[y] = x;
	}
	int ar[n];
	int dis[n];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	while(curc < n && curp < curc){
		if(pt[ar[curc]] == ar[curp]){
			curc++;
		}else{
			curp++;
		}
	}
	if(curc == n){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	unordered_map<int , int> pt;
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		pt[y] = x;
	}
	int ar[n];
	int dis[n];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	if(ar[0] != 1){
		cout << "NO" << endl;
		return 0;
	}
	while(curc < n && curp < curc){
		if(pt[ar[curc]] == ar[curp] || pt[ar[curp]] == ar[curc]){
			curc++;
		}else{
			curp++;
		}
	}
	if(curc == n){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	unordered_map<int , int> pt;
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		pt[y] = x;
	}
	int ar[n];
	int dis[n];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	while(curc < n && curp < curc){
		if(pt[ar[curc]] == ar[curp] || pt[ar[curp]] == ar[curc]){
			curc++;
		}else{
			curp++;
		}
	}
	if(curc == n){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	int pt[n + 10];
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		pt[y] = x;
	}
	int ar[n];
	int dis[n];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	if(ar[0] != 1){
		cout << "NO" << endl;
		return 0;
	}
	while(curc < n && curp < curc){
		if(pt[ar[curc]] == ar[curp] || pt[ar[curp]] == ar[curc]){
			curc++;
		}else{
			curp++;
		}
	}
	if(curc == n){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define EL 200009
vector<int> tree[EL];
int ar[EL + 10];
map<int , int> mp;
vector<int> ans;
bool cmp(int a , int b){
	return mp[a] < mp[b];
}
void bfs(int s){
	queue<int> q;
	q.push(s);
	vector<int> visit(EL , 0);
	while(!q.empty()){
		int t = q.front(); q.pop();
		ans.push_back(t);
		visit[t] = 1;
		for(auto i: tree[t]){
			if(visit[i] != 1){
				q.push(i);
			}
		}

	}
	return;
}
int32_t main()
{
	int n; cin >> n;
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		tree[x].push_back(y);
		tree[y].push_back(x);
	}
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
		mp[ar[i]] = i;
	}
	for(int i = 1; i < n + 1; i++){
		sort(tree[i].begin(), tree[i].end() , cmp);
	}
	bfs(1);
	for(int i = 0;i < n; i++){
		if(ar[i] != ans[i]){
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> tree[200010];
vector<int> level[200010];
void bfs(int s , int l , int pt){
	level[l].push_back(s);
	for(auto i: tree[s]){
		if(i == pt) continue;
		bfs(i , l + 1 , s);
	}
	return;
}
int32_t main()
{
	int n; cin >> n;
	for(int i = 0; i < n - 1; i++){
		int x , y; cin >> x >> y;
		tree[x].push_back(y);
		tree[y].push_back(x);
	}
	bfs(1 , 0 , 0);
	int ar[n];
	for(int i = 0; i < n; i++){
		cin >> ar[i];
	}
	int i = 0;
	int j = 0;
	while(level[i].size() != 0){
		sort(ar + j , ar +j + level[i].size());
		sort(level[i].begin(), level[i].end());
		for(auto k: level[i]){
			if(ar[j] != k){
				cout << "NO" << endl;
				return 0;
			}
			j++;
		}
		i++;
	}
	cout << "YES" << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
	int n; cin >> n;
	set <set<int> >s;
	for(int i = 0; i < n  -1; i++){
		int x , y; cin >> x >> y;
		s.insert({x , y});
	}
	int ar[n + 10];
	for (int i = 0; i < n; ++i)
	{
		cin >> ar[i];
	}
	int curp = 0;
	int curc = 1;
	if(ar[0] != 1){
		cout << "NO" << endl;
		return 0;
	}
	while(curc < n && curp < curc){
		if(s.count({ar[curp] , ar[curc]}) == 1){
			curc++;
		}else{
			curp++;
		}
	}
	if(curp < curc){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}

	return 0;
}


#include "bits/stdc++.h"
using namespace std;

#define  F  first
#define  S  second
#define  mp make_pair
#define  pb push_back
typedef long long int ll;
const int N = 2e5+5,inf = 1e9+7;
set<int>st[N];
int vis[N],Arr[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;cin>>n;
    int m = n-1;
    while(m--)
    {
        int u,v;cin>>u>>v;
        st[u].insert(v);
        st[v].insert(u);
    }
    for(int i=1;i<=n;i++)
        cin>>Arr[i];
    int id = 1;
    queue<int>q1;
    q1.push(1);
    vis[1] = 1;
    if( Arr[id]!=1 )cout<< "No\n",exit(0);
    while(!q1.empty())
    {
        int u = q1.front();
        //if( u!=Arr[id++] )cout<< "No\n",exit(0);
        q1.pop();
        for(auto v:st[u])
        {
            if( id>=n )break;
            if( !vis[Arr[++id]]&&st[u].find(Arr[id])!=st[u].end() )
            {
                vis[Arr[id]]=vis[u]+1;
                q1.push(Arr[id]);
            }
            else if( !vis[Arr[id]] )
            {
                cout<< "No\n",exit(0);
            }
        }
    }
//    for(int i=1;i<n;i++)
//    {
//        if( vis[ Arr[i] ]>vis[ Arr[i+1] ] )
//            cout<< "No\n",exit(0);
//    }
    cout<< "Yes\n";

    // main();

    return 0;
}


#include "bits/stdc++.h"
using namespace std;

#define  F  first
#define  S  second
#define  mp make_pair
#define  pb push_back
typedef long long int ll;
const int N = 1e5+5,inf = 1e9+7;
set<int>st[N];
int vis[N],Arr[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;cin>>n;
    int m = n-1;
    while(m--)
    {
        int u,v;cin>>u>>v;
        st[u].insert(v);
        st[v].insert(u);
    }
    for(int i=1;i<=n;i++)
        cin>>Arr[i];
    int id = 1;
    queue<int>q1;
    q1.push(1);
    vis[1] = 1;
    while(!q1.empty())
    {
        int u = q1.front();
        if( u!=Arr[id++] )cout<< "No\n",exit(0);
        q1.pop();
        for(auto v:st[u])
        {
            if( !vis[v] )vis[v]=1,q1.push(v);
        }
    }
    cout<< "Yes\n";

    // main();

    return 0;
}


#include "bits/stdc++.h"
using namespace std;

#define  F  first
#define  S  second
#define  mp make_pair
#define  pb push_back
typedef long long int ll;
const int N = 2e5+5,inf = 1e9+7;
set<int>st[N];
int vis[N],Arr[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;cin>>n;
    int m = n-1;
    while(m--)
    {
        int u,v;cin>>u>>v;
        st[u].insert(v);
        st[v].insert(u);
    }
    for(int i=1;i<=n;i++)
        cin>>Arr[i];
    int id = 1;
    queue<int>q1;
    q1.push(1);
    vis[1] = 1;
    if( Arr[id]!=1 )cout<< "No\n",exit(0);
    while(!q1.empty())
    {
        int u = q1.front();
        //if( u!=Arr[id++] )cout<< "No\n",exit(0);
        q1.pop();
        for(auto v:st[u])
        {
            if( !vis[v]&&st[u].find(Arr[++id])!=st[u].end() )
            {
                vis[v]=vis[u]+1;
                q1.push(v);
            }
            else if( !vis[v] )
            {
                cout<< "No\n",exit(0);
            }
        }
    }
    for(int i=1;i<n;i++)
    {
        if( vis[ Arr[i] ]>vis[ Arr[i+1] ] )
            cout<< "No\n",exit(0);
    }
    cout<< "Yes\n";

    // main();

    return 0;
}


#include "bits/stdc++.h"
using namespace std;

#define  F  first
#define  S  second
#define  mp make_pair
#define  pb push_back
typedef long long int ll;
const int N = 2e5+5,inf = 1e9+7;
set<int>st[N];
int vis[N],Arr[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;cin>>n;
    int m = n-1;
    while(m--)
    {
        int u,v;cin>>u>>v;
        st[u].insert(v);
        st[v].insert(u);
    }
    for(int i=1;i<=n;i++)
        cin>>Arr[i];
    int id = 1;
    queue<int>q1;
    q1.push(1);
    vis[1] = 1;
    if( Arr[id]!=1 )cout<< "No\n",exit(0);
    while(!q1.empty())
    {
        int u = q1.front();
        //if( u!=Arr[id++] )cout<< "No\n",exit(0);
        q1.pop();
        for(auto v:st[u])
        {
            if( id>=n )break;
            if( !vis[Arr[id+1]]&&st[u].find(Arr[id+1])!=st[u].end() )
            {
                vis[Arr[id+1]]=vis[u]+1;
                q1.push(Arr[id++]);
            }
            else if( !vis[Arr[id]] )
            {
                cout<< "No\n",exit(0);
            }
        }
    }
//    for(int i=1;i<n;i++)
//    {
//        if( vis[ Arr[i] ]>vis[ Arr[i+1] ] )
//            cout<< "No\n",exit(0);
//    }
    cout<< "Yes\n";

    // main();

    return 0;
}


#include "bits/stdc++.h"
using namespace std;

#define  F  first
#define  S  second
#define  mp make_pair
#define  pb push_back
typedef long long int ll;
const int N = 2e5+5,inf = 1e9+7;
set<int>st[N];
int vis[N],Arr[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;cin>>n;
    int m = n-1;
    while(m--)
    {
        int u,v;cin>>u>>v;
        st[u].insert(v);
        st[v].insert(u);
    }
    for(int i=1;i<=n;i++)
        cin>>Arr[i];
    int id = 1;
    queue<int>q1;
    q1.push(1);
    vis[1] = 1;
    if( Arr[id]!=1 )cout<< "No\n",exit(0);
    while(!q1.empty())
    {
        int u = q1.front();
        //if( u!=Arr[id++] )cout<< "No\n",exit(0);
        q1.pop();
        for(auto v:st[u])
        {
            if( id>n )break;
            if( !vis[Arr[++id]]&&st[u].find(Arr[id])!=st[u].end() )
            {
                vis[Arr[id]]=vis[u]+1;
                q1.push(Arr[id]);
            }
            else if( !vis[Arr[id]] )
            {
                cout<< "No\n",exit(0);
            }
        }
    }
//    for(int i=1;i<n;i++)
//    {
//        if( vis[ Arr[i] ]>vis[ Arr[i+1] ] )
//            cout<< "No\n",exit(0);
//    }
    cout<< "Yes\n";

    // main();

    return 0;
}