#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int dcnhan[1001]={0},dccho[1001]={0};
    pair<int,int> dscho[1001],dsnhan[1001];
    int m,c,n,gt,t;
    cin>>n>>m;
    for (int i=0;i<m;i++)
    {
        cin>>c>>t>>gt;
        dcnhan[t]=gt;
        dccho[c]=gt;
    }
    int sl=0,sl2=0;
    for (int i=1;i<=n;i++)
    {
        if (dcnhan[i]==0)
        {
            sl++;
           dsnhan[sl].first=dccho[i];
           dsnhan[sl].second=i;
        }
        if (dccho[i]==0)
        {
            sl2++;
            dscho[sl2].first=dcnhan[i];
            dscho[sl2].second=i;
        }
    }
    sort(dsnhan,dsnhan+sl);
    sort(dscho,dscho+sl);
    cout<<sl<<endl;
    for (int i=1;i<=sl;i++)
    {
        //cout<<dscho[i].first<<" "<<dscho[i].second<<endl;
        cout<<dsnhan[i].second<<" "<<dscho[i].second<<" "<<min(dscho[i].first,dsnhan[i].first)<<endl;
    }
}


#include "float.h"
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(k,a,b) for(int k(a); k < (b); ++k)
#define REP(k,a) for(int k=0; k < (a); ++k)

int main()
{
	int n,p;
	cin >> n >> p;
	if(!p)
	{
		cout << 0 << endl;
		return 0;
	}

	vector<bool> st(n+1,true);
	vector<bool> used(n+1);
	vector<int> edge(n+1),edge2(n+1),d(n+1),mind(n+1);
	st[0]=false;
	REP(i,p)
	{
		int tmpa,tmpb,tmpd;
		cin >> tmpa >> tmpb >> tmpd;
		st[tmpb]=false;
		edge[tmpa]=tmpa;
		edge2[tmpa]=tmpb;
		mind[tmpa]=tmpd;
		d[tmpa]=tmpd;
		used[tmpa]=true;
		used[tmpb]=true;
	}
	REP(i,n+1)
		if(st[i] && !used[i])
			st[i]=false;
	REP(j,n)
	FOR(i,1,n+1)
	if(st[i] && edge[edge2[i]])
	{
			edge2[i]=edge[edge2[i]];
			if(mind[i]>d[edge2[i]] && d[edge2[i]])
				mind[i]=d[edge2[i]];
	}
	int ctr=0;
	REP(i,n+1)
		if(st[i])
			++ctr;
	cout << ctr << endl;
	REP(i,n+1)
		if(st[i])
		{
			cout << i << " " << edge2[i] << " " << mind[i] << endl;
		}
	return 0;
}


#include "float.h"
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(k,a,b) for(int k(a); k < (b); ++k)
#define REP(k,a) for(int k=0; k < (a); ++k)

int main()
{
	int n,p;
	cin >> n >> p;
	if(!p)
	{
		cout << 0 << endl;
		return 0;
	}

	vector<bool> st(n+1,true);
	vector<int> edge(n+1),edge2(n+1),d(n+1),mind(n+1);
	st[0]=false;
	REP(i,p)
	{
		int tmpa,tmpb,tmpd;
		cin >> tmpa >> tmpb >> tmpd;
		st[tmpb]=false;
		edge[tmpa]=tmpb;
		edge2[tmpa]=tmpb;
		mind[tmpa]=tmpd;
		d[tmpa]=tmpd;
	}
	REP(j,n)
	FOR(i,1,n+1)
	if(st[i] && edge[edge2[i]])
	{
			edge2[i]=edge[edge2[i]];
			if(mind[i]>d[edge2[i]])
				mind[i]>d[edge2[i]];
	}
	int ctr=0;
	REP(i,n+1)
		if(st[i])
			++ctr;
	cout << ctr << endl;
	REP(i,n+1)
		if(st[i])
		{
			cout << i << " " << edge2[i] << " " << mind[i] << endl;
		}

	return 0;
}


#include "float.h"
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(k,a,b) for(int k(a); k < (b); ++k)
#define REP(k,a) for(int k=0; k < (a); ++k)

int main()
{
	int n,p;
	cin >> n >> p;
	if(!p)
	{
		cout << 0 << endl;
		return 0;
	}

	vector<bool> st(n+1,true);
	vector<bool> used(n+1);
	vector<int> edge(n+1),edge2(n+1),d(n+1),mind(n+1);
	st[0]=false;
	REP(i,p)
	{
		int tmpa,tmpb,tmpd;
		cin >> tmpa >> tmpb >> tmpd;
		st[tmpb]=false;
		edge[tmpa]=tmpb;
		edge2[tmpa]=tmpb;
		mind[tmpa]=tmpd;
		d[tmpa]=tmpd;
		used[tmpa]=true;
		used[tmpb]=true;
	}
	REP(i,n+1)
		if(st[i] && !used[i])
			st[i]=false;
	REP(j,n)
	FOR(i,1,n+1)
	if(st[i] && edge[edge2[i]])
	{
			edge2[i]=edge[edge2[i]];
			if(mind[i]>d[edge2[i]])
				mind[i]>d[edge2[i]];
	}
	int ctr=0;
	REP(i,n+1)
		if(st[i])
			++ctr;
	cout << ctr << endl;
	REP(i,n+1)
		if(st[i])
		{
			cout << i << " " << edge2[i] << " " << mind[i] << endl;
		}

	return 0;
}


#include "float.h"
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(k,a,b) for(int k(a); k < (b); ++k)
#define REP(k,a) for(int k=0; k < (a); ++k)

int main()
{
	int n,p;
	cin >> n >> p;
	if(!p)
	{
		cout << 0 << endl;
		return 0;
	}

	vector<bool> st(n+1,true);
	vector<bool> used(n+1);
	vector<int> edge(n+1),edge2(n+1),d(n+1),mind(n+1);
	st[0]=false;
	REP(i,p)
	{
		int tmpa,tmpb,tmpd;
		cin >> tmpa >> tmpb >> tmpd;
		st[tmpb]=false;
		edge[tmpa]=tmpb;
		edge2[tmpa]=tmpb;
		mind[tmpa]=tmpd;
		d[tmpa]=tmpd;
		used[tmpa]=true;
		used[tmpb]=true;
	}
	REP(i,n+1)
		if(st[i] && !used[i])
			st[i]=false;
	REP(j,n)
	FOR(i,1,n+1)
	if(st[i] && edge[edge2[i]])
	{
			edge2[i]=edge[edge2[i]];
			if(mind[i]>d[edge2[i]] && d[edge2[i]])
				mind[i]=d[edge2[i]];
	}
	int ctr=0;
	REP(i,n+1)
		if(st[i])
			++ctr;
	cout << ctr << endl;
	REP(i,n+1)
		if(st[i])
		{
			cout << i << " " << edge2[i] << " " << mind[i] << endl;
		}
	return 0;
}


#include "float.h"
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(k,a,b) for(int k(a); k < (b); ++k)
#define REP(k,a) for(int k=0; k < (a); ++k)

int main()
{
	int n,p;
	cin >> n >> p;
	vector<bool> st(n+1,true);
	vector<int> edge(n+1),edge2(n+1),d(n+1),mind(n+1);
	st[0]=false;
	REP(i,p)
	{
		int tmpa,tmpb,tmpd;
		cin >> tmpa >> tmpb >> tmpd;
		st[tmpb]=false;
		edge[tmpa]=tmpb;
		edge2[tmpa]=tmpb;
		mind[tmpa]=tmpd;
		d[tmpa]=tmpd;
	}
	REP(j,n)
	FOR(i,1,n+1)
	if(st[i] && edge[edge2[i]])
	{
			edge2[i]=edge[edge2[i]];
			if(mind[i]>d[edge2[i]])
				mind[i]>d[edge2[i]];
	}
	int ctr=0;
	REP(i,n+1)
		if(st[i])
			++ctr;
	cout << ctr << endl;
	REP(i,n+1)
		if(st[i])
		{
			cout << i << " " << edge2[i] << " " << mind[i] << endl;
		}

	return 0;
}


#include "float.h"
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(k,a,b) for(int k(a); k < (b); ++k)
#define REP(k,a) for(int k=0; k < (a); ++k)

int main()
{
	int n,p;
	cin >> n >> p;
	if(!p)
	{
		cout << 0 << endl;
		return 0;
	}

	vector<bool> st(n+1,true);
	vector<bool> used(n+1);
	vector<int> edge(n+1),edge2(n+1),d(n+1),mind(n+1);
	st[0]=false;
	REP(i,p)
	{
		int tmpa,tmpb,tmpd;
		cin >> tmpa >> tmpb >> tmpd;
		st[tmpb]=false;
		edge[tmpa]=tmpb;
		edge2[tmpa]=tmpb;
		mind[tmpa]=tmpd;
		d[tmpa]=tmpd;
		used[tmpa]=true;
		used[tmpb]=true;
	}
	REP(i,n+1)
		if(st[i] && !used[i])
			st[i]=false;
	FOR(i,1,n+1)
	if(st[i])
	{
		if(mind[i]>d[edge2[i]] && d[edge2[i]])
			mind[i]=d[edge2[i]];
		while(edge[edge2[i]])
		{
			edge2[i]=edge[edge2[i]];
			if(mind[i]>d[edge2[i]] && d[edge2[i]])
				mind[i]=d[edge2[i]];
		}
	}
	int ctr=0;
	REP(i,n+1)
		if(st[i])
			++ctr;
	cout << ctr << endl;
	REP(i,n+1)
		if(st[i])
		{
			cout << i << " " << edge2[i] << " " << mind[i] << endl;
		}
	return 0;
}


// In the name of God

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1000+10;

vector<pair<int,int> > v[maxn];
int p[maxn];
vector<pair<pair<int,int> ,int> > ans;

int main()
{
  int n;
  cin >> n;
  int m;
  cin >> m;
  for(int i=0;i<m;++i)
    {
      int v1,v2,w;
      cin >> v1 >> v2 >> w;
      v[v1].push_back(make_pair(v2,w));
      p[v2] = v1;
    }
  if(n == 1)
    {
      cout << 0 << endl;
      return 0;
    }
  for(int i=1;i<n;++i)
    {
      if(p[i] == 0 and v[i].size() > 0)
	{
	  pair<int,int> help;
	  help.first = i;
	  help.second = i;
	  int qw = 100000000;
	  while(1)
	    {
	      if(v[help.second].size() > 0){
		if(qw > v[help.second][0].second)
		  qw = v[help.second][0].second;
		help.second = v[help.second][0].first;
	      }
	      else
		break;
	    }
	  ans.push_back(make_pair(help,qw));
	}
    }
  if(ans.size() == 0)
    {
      cout << 0 << endl;
      return 0;
    }
  sort(ans.begin(),ans.end());
  cout << ans.size() << endl;
  for(int i=0;i<ans.size();++i)
    cout << ans[i].first.first <<  " " << ans[i].first.second << " " << ans[i].second  << endl;
  return 0;
}


// In the name of God

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1000+10;

vector<pair<int,int> > v[maxn];
int p[maxn];
vector<pair<pair<int,int> ,int> > ans;

bool cmp(pair<pair<int,int> ,int> fi,pair<pair<int,int> ,int> se)
{
  return fi.first.first < se.first.first;
}

int main()
{
  int n;
  cin >> n;
  int m;
  cin >> m;
  for(int i=0;i<m;++i)
    {
      int v1,v2,w;
      cin >> v1 >> v2 >> w;
      v[v1].push_back(make_pair(v2,w));
      p[v2] = v1;
    }
  if(n == 1 or m == 0)
    {
      cout << 0 << endl;
      return 0;
    }
  for(int i=1;i<=n;++i)
    {
      if(p[i] == 0 and v[i].size() > 0)
	{
	  pair<int,int> help;
	  help.first = i;
	  help.second = i;
	  int qw = 100000000;
	  while(1)
	    {
	      if(v[help.second].size() > 0){
		if(qw > v[help.second][0].second)
		  qw = v[help.second][0].second;
		help.second = v[help.second][0].first;
	      }
	      else
		break;
	    }
	  if(help.first != help.second)
	    ans.push_back(make_pair(help,qw));
	}
    }
  if(ans.size() == 0)
    {
      cout << 0 << endl;
      return 0;
    }
  sort(ans.begin(),ans.end(),cmp);
  cout << ans.size() << endl;
  for(int i=0;i<ans.size();++i)
    cout << ans[i].first.first <<  " " << ans[i].first.second << " " << ans[i].second  << endl;
  return 0;
}


// In the name of God

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1000+10;

vector<pair<int,int> > v[maxn];
int p[maxn];
vector<pair<pair<int,int> ,int> > ans;

int main()
{
  int n;
  cin >> n;
  int m;
  cin >> m;
  for(int i=0;i<m;++i)
    {
      int v1,v2,w;
      cin >> v1 >> v2 >> w;
      v[v1].push_back(make_pair(v2,w));
      p[v2] = v1;
    }
  if(n == 1)
    {
      cout << 0 << endl;
      return 0;
    }
  for(int i=1;i<n;++i)
    {
      if(p[i] == 0)
	{
	  pair<int,int> help;
	  help.first = i;
	  help.second = i;
	  int qw = 100000000;
	  while(1)
	    {
	      if(v[help.second].size() > 0){
		if(qw > v[help.second][0].second)
		  qw = v[help.second][0].second;
		help.second = v[help.second][0].first;
	      }
	      else
		break;
	    }
	  ans.push_back(make_pair(help,qw));
	}
    }
  if(ans.size() == 0)
    {
      cout << 0 << endl;
      return 0;
    }
  sort(ans.begin(),ans.end());
  cout << ans.size() << endl;
  for(int i=0;i<ans.size();++i)
    cout << ans[i].first.first <<  " " << ans[i].first.second << " " << ans[i].second  << endl;
  return 0;
}


// In the name of God

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1000+10;

vector<pair<int,int> > v[maxn];
int p[maxn];
vector<pair<pair<int,int> ,int> > ans;

bool cmp(pair<pair<int,int> ,int> fi,pair<pair<int,int> ,int> se)
{
  return fi.first.first < se.first.first;
}

int main()
{
  int n;
  cin >> n;
  int m;
  cin >> m;
  for(int i=0;i<m;++i)
    {
      int v1,v2,w;
      cin >> v1 >> v2 >> w;
      v[v1].push_back(make_pair(v2,w));
      p[v2] = v1;
    }
  if(n == 1)
    {
      cout << 0 << endl;
      return 0;
    }
  for(int i=1;i<n;++i)
    {
      if(p[i] == 0 and v[i].size() > 0)
	{
	  pair<int,int> help;
	  help.first = i;
	  help.second = i;
	  int qw = 100000000;
	  while(1)
	    {
	      if(v[help.second].size() > 0){
		if(qw > v[help.second][0].second)
		  qw = v[help.second][0].second;
		help.second = v[help.second][0].first;
	      }
	      else
		break;
	    }
	  if(help.first != help.second)
	    ans.push_back(make_pair(help,qw));
	}
    }
  if(ans.size() == 0)
    {
      cout << 0 << endl;
      return 0;
    }
  sort(ans.begin(),ans.end(),cmp);
  cout << ans.size() << endl;
  for(int i=0;i<ans.size();++i)
    cout << ans[i].first.first <<  " " << ans[i].first.second << " " << ans[i].second  << endl;
  return 0;
}


#include<stdio.h>
int house[1002][3],done[1002],ans[1002][3];
int main()
{
    int next,n,p,min=1000000,i,j,inter[1000][3],tank,tap,dia,count=0;
    scanf("%d%d",&n,&p);
    for(i=0;i<p;i++)
    {
        scanf("%d%d%d",&inter[i][0],&inter[i][1],&inter[i][2]);
        house[inter[i][0]][0]=inter[i][1];
        house[inter[i][1]][1]=inter[i][0];
        house[inter[i][0]][2]=inter[i][2];
    }
//  for(i=1;i<=n;i++)
//      printf("%d %d %d\n",house[i][0],house[i][1],house[i][2]);
//  printf("\n");
    for(i=1;i<=n;i++)
    {
        if(done[i])
            continue;
        if(house[i][1]==0)
        {
            min=house[i][2];
            tank=i;
            next=house[i][0];
            done[i]=1;
            while(house[next][0]!=0)
            {
                if(done[next])
                    break;
                if(house[next][2]<min)
                    min=house[next][2];
                done[next]=1;
    //          printf("next:%d\n",next);
                next=house[next][0];
            }
            if(done[next])
                continue;
            count++;
    //      printf("count:%d min:%d\n",count,min);
            ans[count][0]=tank;
            ans[count][1]=next;
            ans[count][2]=min;
    //      printf("%d %d %d\n",ans[count][0],ans[count][1],ans[count][2]);
        }
    }
    printf("%d\n",count);
    for(i=1;i<=count;i++)
        printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
    return 0;
}


#include<stdio.h>
int house[1002][3],done[1002],ans[1002][3];
int main()
{
    int next,n,p,min=1000000,i,j,inter[1000][3],tank,tap,dia,count=0;
    scanf("%d%d",&n,&p);
    for(i=0;i<p;i++)
    {
        scanf("%d%d%d",&inter[i][0],&inter[i][1],&inter[i][2]);
        house[inter[i][0]][0]=inter[i][1];
        house[inter[i][1]][1]=inter[i][0];
        house[inter[i][0]][2]=inter[i][2];
    }
//  for(i=1;i<=n;i++)
//      printf("%d %d %d\n",house[i][0],house[i][1],house[i][2]);
//  printf("\n");
    if(p==0)
    {
        printf("0\n");
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        if(done[i]||(house[i][0]==0 && house[i][1]==0))
            continue;
        if(house[i][1]==0)
        {
            min=house[i][2];
            tank=i;
            next=house[i][0];
            done[i]=1;
            while(house[next][0]!=0)
            {
                if(done[next])
                    break;
                if(house[next][2]<min)
                    min=house[next][2];
                done[next]=1;
    //          printf("next:%d\n",next);
                next=house[next][0];
            }
            if(done[next])
                continue;
            count++;
    //      printf("count:%d min:%d\n",count,min);
            ans[count][0]=tank;
            ans[count][1]=next;
            ans[count][2]=min;
    //      printf("%d %d %d\n",ans[count][0],ans[count][1],ans[count][2]);
        }
    }
    printf("%d\n",count);
    for(i=1;i<=count;i++)
        printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
    return 0;
}


#include<stdio.h>
int house[1002][3],done[1002],ans[1002][3];
int main()
{
    int next,n,p,min=1000000,i,j,inter[1000][3],tank,tap,dia,count=0;
    scanf("%d%d",&n,&p);
    for(i=0;i<p;i++)
    {
        scanf("%d%d%d",&inter[i][0],&inter[i][1],&inter[i][2]);
        house[inter[i][0]][0]=inter[i][1];
        house[inter[i][1]][1]=inter[i][0];
        house[inter[i][0]][2]=inter[i][2];
    }
//  for(i=1;i<=n;i++)
//      printf("%d %d %d\n",house[i][0],house[i][1],house[i][2]);
//  printf("\n");
    if(p==0)
    {
        printf("0\n");
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        if(done[i])
            continue;
        if(house[i][1]==0)
        {
            min=house[i][2];
            tank=i;
            next=house[i][0];
            done[i]=1;
            while(house[next][0]!=0)
            {
                if(done[next])
                    break;
                if(house[next][2]<min)
                    min=house[next][2];
                done[next]=1;
    //          printf("next:%d\n",next);
                next=house[next][0];
            }
            if(done[next])
                continue;
            count++;
    //      printf("count:%d min:%d\n",count,min);
            ans[count][0]=tank;
            ans[count][1]=next;
            ans[count][2]=min;
    //      printf("%d %d %d\n",ans[count][0],ans[count][1],ans[count][2]);
        }
    }
    printf("%d\n",count);
    for(i=1;i<=count;i++)
        printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
    return 0;
}


#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornd(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define forabd(i, b, a) for (int i = (int)(b); i >= (int)(a); i--)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define re return

using namespace std;

int a[1001][1001];

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt","w", stdout);
    zero(a);
    int n, p;
    cin >> n >> p;

    forn(i, p){
        int a1, b1, d1;
        cin >> a1 >> b1 >> d1;
        a[a1][b1] = d1;
    }
    vector <pair<int, pair <int, int> > > ans;
    forab(i, 1, n){
        bool flag = true;
        forab(j, 1, n)
            if (a[j][i] != 0){
                flag = false;
                break;
            }
        if (flag == true){
            int e = 1e9, v = i, index = -1;
            while (true){
                bool f = false;
                forab(k, 1, n)
                    if (a[v][k] != 0){
                        e = min(e, a[v][k]);
                        v = k;
                        f = true;
                        index = k;
                        break;
                    }
                if (f == false){
                    break;
                }
            }
            if (e != 1e9){
                ans.pb(mp(i, mp(index, e)));
            }

        }
    }
    cout << ans.size() << endl;
    forn(i, ans.size())
        cout << ans[i].first << " " << ans[i].second.first << " " << ans[i].second.second << endl;

    cin.get();
    cin.get();
    return 0;
}


#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornd(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define forabd(i, b, a) for (int i = (int)(b); i >= (int)(a); i--)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define re return

using namespace std;

int a[1001][1001];

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt","w", stdout);
    zero(a);
    int n, p;
    cin >> n >> p;

    forn(i, p){
        int a1, b1, d1;
        cin >> a1 >> b1 >> d1;
        a[a1][b1] = d1;
    }
    vector <pair<int, pair <int, int> > > ans;
    forab(i, 1, n){
        bool flag = true;
        forab(j, 1, n)
            if (a[j][i] != 0){
                flag = false;
                break;
            }
        if (flag == true){
            int e = 1e9, v = i, index = -1;
            while (true){
                bool f = false;
                forab(k, 1, n)
                    if (a[v][k] != 0){
                        e = min(e, a[v][k]);
                        v = k;
                        f = true;
                        index = k;
                        break;
                    }
                if (f == false){
                    break;
                }
            }
            if (e != 1e9){
                ans.pb(mp(i, mp(index, e)));
            }

        }
    }
    cout << ans.size();
    forn(i, ans.size())
        cout << ans[i].first << " " << ans[i].second.first << " " << ans[i].second.second << endl;

    cin.get();
    cin.get();
    return 0;
}


#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornd(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define forabd(i, b, a) for (int i = (int)(b); i >= (int)(a); i--)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define re return

using namespace std;

int a[1001][1001];

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt","w", stdout);
    zero(a);
    int n, p;
    cin >> n >> p;

    forn(i, p){
        int a1, b1, d1;
        cin >> a1 >> b1 >> d1;
        a[a1][b1] = d1;
    }
    vector <pair<int, pair <int, int> > > ans;
    forab(i, 1, n){
        bool flag = true;
        forab(j, 1, n)
            if (a[j][i] != 0){
                flag = false;
                break;
            }
        if (flag == true){
            int e = 1e9, v = i, index = -1;
            while (true){
                bool f = false;
                forab(k, 1, n)
                    if (a[v][k] != 0){
                        v = k;
                        e = min(e, a[i][k]);
                        f = true;
                        index = k;
                        break;
                    }
                if (f == false){
                    break;
                }
            }
            if (e != 1e9){
                ans.pb(mp(i, mp(index, e)));
            }

        }
    }
    cout << ans.size();
    forn(i, ans.size())
        cout << ans[i].first << " " << ans[i].second.first << " " << ans[i].second.second << endl;

    cin.get();
    return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

int N, P, u, v, _w;
int Q[1024][3], nQ = 0;
int nxt[1024], w[1024];
bool pre[1024];

void calc(int u)
{
    int maxf = -1;
    Q[nQ][0] = u;
    while( nxt[u] != -1 ) {
        if( maxf == -1 || maxf > w[u] ) maxf = w[u];
        u = nxt[u];
    }
    Q[nQ][1] = u; Q[nQ++][2] = maxf;
}

int main()
{
    scanf("%d %d", &N, &P);
    memset(pre, 0, sizeof(pre));
    memset(nxt, -1, sizeof(nxt));
    for(int i = 0; i < P; ++i) {
        scanf("%d %d %d", &u, &v, &_w);
        pre[v] = 1, nxt[u] = v, w[u] = _w;
    }
    for(int i = 1; i <= N; ++i)
        if( !pre[i] ) calc(i);
    printf("%d\n", nQ);
    for(int i = 0; i < nQ; ++i)
        printf("%d %d %d\n", Q[i][0], Q[i][1], Q[i][2]);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

int N, P, u, v, _w;
int Q[1024][3], nQ = 0;
int nxt[1024], w[1024];
bool pre[1024];

void calc(int u)
{
    int maxf = 0x7fffffff;
    Q[nQ][0] = u;
    while( nxt[u] != -1 ) {
        maxf = min(maxf, w[u]);
        u = nxt[u];
    }
    Q[nQ][1] = u; Q[nQ++][2] = maxf;
}

int main()
{
    scanf("%d %d", &N, &P);
    memset(pre, 0, sizeof(pre));
    memset(nxt, -1, sizeof(nxt));
    for(int i = 0; i < P; ++i) {
        scanf("%d %d %d", &u, &v, &_w);
        pre[v] = 1, nxt[u] = v, w[u] = _w;
    }
    for(int i = 1; i <= N; ++i)
        if( !pre[i] && nxt[i] != -1 ) calc(i);
    printf("%d\n", nQ);
    for(int i = 0; i < nQ; ++i)
        printf("%d %d %d\n", Q[i][0], Q[i][1], Q[i][2]);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

int N, P, u, v, _w;
int Q[1024][3], nQ = 0;
int nxt[1024], w[1024];
bool pre[1024];

void calc(int u)
{
    int maxf = 0x7fffffff;
    Q[nQ][0] = u;
    while( nxt[u] != -1 ) {
        maxf = min(maxf, w[u]);
        u = nxt[u];
    }
    Q[nQ][1] = u; Q[nQ++][2] = maxf;
}

int main()
{
    scanf("%d %d", &N, &P);
    memset(pre, 0, sizeof(pre));
    memset(nxt, -1, sizeof(nxt));
    for(int i = 0; i < P; ++i) {
        scanf("%d %d %d", &u, &v, &_w);
        pre[v] = 1, nxt[u] = v, w[u] = _w;
    }
    for(int i = 1; i <= N; ++i)
        if( !pre[i] ) calc(i);
    printf("%d\n", nQ);
    for(int i = 0; i < nQ; ++i)
        printf("%d %d %d\n", Q[i][0], Q[i][1], Q[i][2]);
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>
#include <time.h>
#include <string.h>
using namespace std;

int lowbit(int t){return t&(-t);}
int countbit(int t){return (t==0)?0:(1+countbit(t&(t-1)));}
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}
#define LL long long
#define PI acos(-1.0)
#define N  1001
#define Max INT_MAX
#define Min INT_MIN
#define eps 1e-8
#define FRE freopen("a.txt","r",stdin)

int index[N];
int w[N];
int in[N];
int out[N];
int n,m;
struct node{
    int a,b,c;
};
queue<node> q;
void init(){
    int i,j;
    for(i=0;i<=n;i++){
        in[i]=0;
        out[i]=0;
        w[i]=Max;
        index[i]=0;
    }
    while(!q.empty())q.pop();
}
void dfs(int x){
    int minm=w[x];
    node tmp;
    tmp.a=x;
    x=index[x];
    while(index[x]){
        int t=index[x];
        minm=min(minm,w[t]);
        x=index[x];
    }
    tmp.b=x,tmp.c=minm;
    q.push(tmp);
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        int i,j;
        init();
        for(i=1;i<=m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            index[a]=b;
            w[a]=c;
            out[a]++;
            in[b]++;
        }
        int cnt=0;
        for(i=1;i<=n;i++){
            if(!in[i] && out[i])
            {dfs(i);cnt++;}
        }
        printf("%d\n",cnt);
        while(!q.empty()){
            node tmp=q.front();
            q.pop();
            printf("%d %d %d\n",tmp.a,tmp.b,tmp.c);
        }
    }
    return 0;
}

/*
int a[1001];
int main(){FRE;
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        int i,j;
        int sum=0;
        for(i=1;i<=m;i++){
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum<n){printf("-1.0\n");continue;}
        int sum2=sum-a[k];
        double p=1.0;
        for(i=1;i<=n-1;i++){
            p=p*double(n-1)/
*/
/*
int a[N];
map<int,int> m;
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int i,j;
        m.clear();
        for(i=0;i<n;i++){

            scanf("%d",&a[i]);
            m[a[i]]=1;
        }
        sort(a,a+n);
        int tag=0;
        for(i=0;i<n-1;i++)
        if(!m[2*a[i]]){tag=1;break;}
        if(tag)puts("YES");
        else
        puts("NO");
    }
    return 0;
}
*/

/*
bool chk(int a,int b){
    if(a==0&&b==0)return true;
    if(a%10==b/10 && a/10==b%10)return true;
    return false;
}

int main(){
    int a,b;
    while(scanf("%d:%d",&a,&b)!=EOF){
        int tag=0;
        b++;
        int i,j;
            for(i=a;a<=24;a++)
            {
                if(a==24)a=0;
                for(j=b;b<=60;b++)
                {
                    if(b==60){b=0;break;}
                    if(chk(a,b)){tag=1;break;}
                }
                if(tag)break;
            }
            if(a==0)printf("00:");
            else
            printf("%02d:",a);
            if(b==0)
            printf("00\n");
            else
            printf("%02d\n",b);
    }
    return 0;
}
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>
#include <time.h>
#include <string.h>
using namespace std;

int lowbit(int t){return t&(-t);}
int countbit(int t){return (t==0)?0:(1+countbit(t&(t-1)));}
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}
#define LL long long
#define PI acos(-1.0)
#define N  100010
#define Max INT_MAX
#define Min INT_MIN
#define eps 1e-8
#define FRE freopen("a.txt","r",stdin)
vector<int> v[1001];
int g[1001][1001];
int in[1001];
int vis[1001];
int n,m;
int cnt;
struct node{
    int l,r;
    int val;
}p[N];
void dfs(int x,int cnt,int minm){
    int i,j;
    for(i=0;i<v[x].size();i++){
        int t=v[x][i];
        int tmp=g[x][t];
        if(tmp<minm)minm=tmp;
        p[cnt].r=t;
        p[cnt].val=minm;
        dfs(t,cnt,minm);
    }
}

void topsort(){
    int i,j;
    queue<int> q;

    for(i=1;i<=n;i++)
    if(in[i]==0)
    q.push(i);
    cnt=0;
    if(q.size()==0){return ;}

    while(!q.empty()){
        int x=q.front();
        q.pop();
        p[cnt].l=x;
        int minm=Max;
        dfs(x,cnt,minm);
        cnt++;
    }
}
bool cmp(node a,node b){
    return a.l<b.l;
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        int i,j;
        memset(in,0,sizeof(in));
        memset(g,0,sizeof(g));
        for(i=0;i<=n;i++)v[i].clear();
       // memset(vis,0,sizeof(vis));
        for(i=1;i<=m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            in[b]++;
            v[a].push_back(b);
            g[a][b]=c;
        }
        topsort();
        sort(p,p+cnt,cmp);
        printf("%d\n",cnt);
        for(i=0;i<cnt;i++)
        printf("%d %d %d\n",p[i].l,p[i].r,p[i].val);
    }
    return 0;
}
/*
int a[N];
map<int,int> m;
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int i,j;
        m.clear();
        for(i=0;i<n;i++){

            scanf("%d",&a[i]);
            m[a[i]]=1;
        }
        sort(a,a+n);
        int tag=0;
        for(i=0;i<n-1;i++)
        if(!m[2*a[i]]){tag=1;break;}
        if(tag)puts("YES");
        else
        puts("NO");
    }
    return 0;
}
*/

/*
bool chk(int a,int b){
    if(a==0&&b==0)return true;
    if(a%10==b/10 && a/10==b%10)return true;
    return false;
}

int main(){
    int a,b;
    while(scanf("%d:%d",&a,&b)!=EOF){
        int tag=0;
        b++;
        int i,j;
            for(i=a;a<=24;a++)
            {
                if(a==24)a=0;
                for(j=b;b<=60;b++)
                {
                    if(b==60){b=0;break;}
                    if(chk(a,b)){tag=1;break;}
                }
                if(tag)break;
            }
            if(a==0)printf("00:");
            else
            printf("%02d:",a);
            if(b==0)
            printf("00\n");
            else
            printf("%02d\n",b);
    }
    return 0;
}
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>
#include <time.h>
#include <string.h>
using namespace std;

int lowbit(int t){return t&(-t);}
int countbit(int t){return (t==0)?0:(1+countbit(t&(t-1)));}
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}
#define LL long long
#define PI acos(-1.0)
#define N  1001
#define Max INT_MAX
#define Min INT_MIN
#define eps 1e-8
#define FRE freopen("a.txt","r",stdin)

int index[N];
int w[N];
int in[N];
int out[N];
int n,m;
struct node{
    int a,b,c;
};
queue<node> q;
void init(){
    int i,j;
    for(i=0;i<=n;i++){
        in[i]=0;
        out[i]=0;
        w[i]=Max;
        index[i]=0;
    }
    while(!q.empty())q.pop();
}
void dfs(int x){
    int minm=w[x];
    node tmp;
    tmp.a=x;
    //x=index[x];
    while(index[x]){
        int t=index[x];
        minm=min(minm,w[t]);
        x=index[x];
    }
    tmp.b=x,tmp.c=minm;
    q.push(tmp);
}
int main(){//FRE;
    while(scanf("%d%d",&n,&m)!=EOF){
        int i,j;
        init();
        for(i=1;i<=m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            index[a]=b;
            w[a]=c;
            out[a]++;
            in[b]++;
        }
        int cnt=0;
        for(i=1;i<=n;i++){
            if(!in[i] && out[i])
            {dfs(i);cnt++;}
        }
        printf("%d\n",cnt);
        while(!q.empty()){
            node tmp=q.front();
            q.pop();
            printf("%d %d %d\n",tmp.a,tmp.b,tmp.c);
        }
    }
    return 0;
}

/*
int a[1001];
int main(){FRE;
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF){
        int i,j;
        int sum=0;
        for(i=1;i<=m;i++){
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum<n){printf("-1.0\n");continue;}
        int sum2=sum-a[k];
        double p=1.0;
        for(i=1;i<=n-1;i++){
            p=p*double(n-1)
*/


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<string.h>
using namespace std;
#define ff(i,a,b)  for(int i=a;i<=b;i++)
#define fil(x,y) memset(x,y,sizeof(x))
#define fre1     freopen("in.txt","r",stdin)
#define fre2     freopen("out.txt","w",stdout)
#define ma  1005
#define mb  1000002
#define max_int 999999999
typedef long long ll;
struct Edge{
    int end,next;
    int value;
}edge[mb];
struct ANS{
int a,b,c;
}ans[ma];
int head[ma],cnt;
void initial()
{fil(head,-1);cnt=0;}
int pre[ma],num[ma],road[ma],id[ma];
int cu[ma];
int n,m,N;
int sink,source;
void add(int  st,int ed,int value)
{
    edge[cnt].end=ed;edge[cnt].value=value;
    edge[cnt].next=head[st];head[st]=cnt++;
    edge[cnt].end=st;edge[cnt].value=0;
    edge[cnt].next=head[ed];
    head[ed]=cnt++;
}
/*
 *
 */

ll isap()
{//sink=????+1,source=0
   int N=sink+1;
   ff(i,0,N) {
      num[i]=id[i]=0;
      cu[i]=head[i];
   }
  ll max_=0;
  num[0]=N;
  int now=pre[source]=source;
  int flow=999999999;
  while(id[source]<N)
  {
      loop: for(int &i=cu[now];i!=-1;i=edge[i].next)
      {
          if(edge[i].value==0) continue;
          int v=edge[i].end;
          if(id[now]==id[v]+1)
          {
              pre[v]=now;now=v;
              road[v]=i;
              flow=min(flow,edge[i].value);
              if(sink==now)
              {
                  max_+=(ll)flow;
                  for(int vv=sink;vv!=source;vv=pre[vv])
                  {
                      edge[road[vv]].value-=flow;
                      edge[road[vv]^1].value+=flow;
                  }
                  now=source;
                  flow=max_int;
              }
              goto loop;

          }
      }
      int T=N;
      for(int i=head[now];i!=-1;i=edge[i].next)
      {
          if(edge[i].value==0)continue;
          int v=edge[i].end;
          if(id[v]<T){T=id[v];cu[ now]=i;}
      }
      if( (--num[id[now]])==0  ) break;
      num[ (id[now]=T+1)  ]++;
      now=pre[now];
  }
  return max_;
  // cout<<max_<<endl;
}
int in[ma],out[ma];
int f[ma],r[ma];
int fa[ma];
int F(int x){
 if(x!=f[x]) f[x]=F(f[x]);
 return f[x];
}
bool cmp2(ANS aa,ANS bb){
 return aa.a<aa.b;
}
void U(int x,int y){
 x=F(x),y=F(y);
 if(x!=y){
   r[x]+=r[y];
   f[y]=x;
 }
}
int ou[ma];
int main()
{
  while(cin>>n>>m){
    initial();
    ff(i,1,n){f[i]=i,r[i]=1;}
    fil(in,0);
    fil(out,0);
    ff(i,1,m){
      int a,c,b;
      cin>>a>>b>>c;
      out[a]++;
      in[b]++;
      add(a,b,c);
      int x=F(a);
      int y=F(b);
      if(x!=y) U(x,y);
    }
    source=0;
    sink=n+1;
    ou[0]=0;
    ff(i,1,n){
      if(!in[i]) add(source,i,999999999);
      else if(!out[i])
      {
       add(i,sink,max_int);
       ou[++ou[0]]=i;
       fa[i]=F(i);
      }
    }
    ll k=isap();

    if(!k){cout<<k<<endl;continue;}
    int p=0;
    for(int i=head[source];i!=-1;i=edge[i].next){
       if(edge[i].value==max_int) continue;
       int x=edge[i^1].value;
       int s=edge[i].end;
       int faa=F(s);
       for(int j=1;j<=ou[0];j++){
         if(fa[ou[j]]==faa){
           ans[++p].a=s;
           ans[p].b=ou[j];
           ans[p].c=x;
           break;
         }
       }

    }

    sort(ans+1,ans+p+1,cmp2);
    cout<<p<<endl;
    ff(i,1,p){

     cout<<ans[i].a<<" "<<ans[i].b<<" "<<ans[i].c<<endl;
    }
  }
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<string.h>
using namespace std;
#define ff(i,a,b)  for(int i=a;i<=b;i++)
#define fil(x,y) memset(x,y,sizeof(x))
#define fre1     freopen("in.txt","r",stdin)
#define fre2     freopen("out.txt","w",stdout)
#define ma  1005
#define mb  1000002
#define max_int 999999999
struct Edge{
    int end,next;
    int value;
}edge[mb];
struct ANS{
int a,b,c;
}ans[ma];
int head[ma],cnt;
void initial()
{fil(head,-1);cnt=0;}
int pre[ma],num[ma],road[ma],id[ma];
int cu[ma];
int n,m,N;
int sink,source;
void add(int  st,int ed,int value)
{
    edge[cnt].end=ed;edge[cnt].value=value;
    edge[cnt].next=head[st];head[st]=cnt++;
    edge[cnt].end=st;edge[cnt].value=0;
    edge[cnt].next=head[ed];
    head[ed]=cnt++;
}
/*
 *
 */

int isap()
{//sink=????+1,source=0
   int N=sink+1;
   ff(i,0,N) {
      num[i]=id[i]=0;
      cu[i]=head[i];
   }
  int max_=0;
  num[0]=N;
  int now=pre[source]=source;
  int flow=max_int;
  while(id[source]<N)
  {
      loop: for(int &i=cu[now];i!=-1;i=edge[i].next)
      {
          if(edge[i].value==0) continue;
          int v=edge[i].end;
          if(id[now]==id[v]+1)
          {
              pre[v]=now;now=v;
              road[v]=i;
              flow=min(flow,edge[i].value);
              if(sink==now)
              {
                  max_+=flow;
                  for(int vv=sink;vv!=source;vv=pre[vv])
                  {
                      edge[road[vv]].value-=flow;
                      edge[road[vv]^1].value+=flow;
                  }
                  now=source;
                  flow=max_int;
              }
              goto loop;

          }
      }
      int T=N;
      for(int i=head[now];i!=-1;i=edge[i].next)
      {
          if(edge[i].value==0)continue;
          int v=edge[i].end;
          if(id[v]<T){T=id[v];cu[ now]=i;}
      }
      if( (--num[id[now]])==0  ) break;
      num[ (id[now]=T+1)  ]++;
      now=pre[now];
  }
  return max_;
  // cout<<max_<<endl;
}
int in[ma],out[ma];
int f[ma],r[ma];
int fa[ma];
int F(int x){
 if(x!=f[x]) f[x]=F(f[x]);
 return f[x];
}
bool cmp2(ANS aa,ANS bb){
 return aa.a<aa.b;
}
void U(int x,int y){
 x=F(x),y=F(y);
 if(x!=y){
   r[x]+=r[y];
   f[y]=x;
 }
}
int ou[ma];
int main()
{
  while(cin>>n>>m){
    initial();
    ff(i,1,n){f[i]=i,r[i]=1;}
    fil(in,0);
    fil(out,0);
    ff(i,1,m){
      int a,c,b;
      cin>>a>>b>>c;
      out[a]++;
      in[b]++;
      add(a,b,c);
      int x=F(a);
      int y=F(b);
      if(x!=y) U(x,y);
    }
    source=0;
    sink=n+1;
    ou[0]=0;
    ff(i,1,n){
      if(!in[i]) add(source,i,max_int);
      else if(!out[i])
      {
       add(i,sink,max_int);
       ou[++ou[0]]=i;
       fa[i]=F(i);
      }
    }
    int k=isap();

    if(!k){cout<<k<<endl;continue;}
    int p=0;
    for(int i=head[source];i!=-1;i=edge[i].next){
       if(edge[i].value==max_int) continue;
       int x=edge[i^1].value;
       int s=edge[i].end;
       int faa=F(s);
       for(int j=1;j<=ou[0];j++){
         if(fa[ou[j]]==faa){
           ans[++p].a=s;
           ans[p].b=ou[j];
           ans[p].c=x;
           break;
         }
       }

    }

    sort(ans+1,ans+p+1,cmp2);
    cout<<p<<endl;
    ff(i,1,p){

     cout<<ans[i].a<<" "<<ans[i].b<<" "<<ans[i].c<<endl;
    }
  }
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<string.h>
using namespace std;
#define ff(i,a,b)  for(int i=a;i<=b;i++)
#define fil(x,y) memset(x,y,sizeof(x))
#define fre1     freopen("in.txt","r",stdin)
#define fre2     freopen("out.txt","w",stdout)
#define ma  1005
#define mb  1000002
#define max_int 999999999
typedef long long ll;
struct Edge{
    int end,next;
    int value;
}edge[mb];
struct ANS{
int a,b,c;
}ans[ma];
int head[ma],cnt;
void initial()
{fil(head,-1);cnt=0;}
int pre[ma],num[ma],road[ma],id[ma];
int cu[ma];
int n,m,N;
int sink,source;
void add(int  st,int ed,int value)
{
    edge[cnt].end=ed;edge[cnt].value=value;
    edge[cnt].next=head[st];head[st]=cnt++;
    edge[cnt].end=st;edge[cnt].value=0;
    edge[cnt].next=head[ed];
    head[ed]=cnt++;
}
/*
 *
 */

ll isap()
{//sink=????+1,source=0
   int N=sink+1;
   ff(i,0,N) {
      num[i]=id[i]=0;
      cu[i]=head[i];
   }
  ll max_=0;
  num[0]=N;
  int now=pre[source]=source;
  int flow=999999999;
  while(id[source]<N)
  {
      loop: for(int &i=cu[now];i!=-1;i=edge[i].next)
      {
          if(edge[i].value==0) continue;
          int v=edge[i].end;
          if(id[now]==id[v]+1)
          {
              pre[v]=now;now=v;
              road[v]=i;
              flow=min(flow,edge[i].value);
              if(sink==now)
              {
                  max_+=(ll)flow;
                  for(int vv=sink;vv!=source;vv=pre[vv])
                  {
                      edge[road[vv]].value-=flow;
                      edge[road[vv]^1].value+=flow;
                  }
                  now=source;
                  flow=max_int;
              }
              goto loop;

          }
      }
      int T=N;
      for(int i=head[now];i!=-1;i=edge[i].next)
      {
          if(edge[i].value==0)continue;
          int v=edge[i].end;
          if(id[v]<T){T=id[v];cu[ now]=i;}
      }
      if( (--num[id[now]])==0  ) break;
      num[ (id[now]=T+1)  ]++;
      now=pre[now];
  }
  return max_;
  // cout<<max_<<endl;
}
int in[ma],out[ma];
int f[ma],r[ma];
int fa[ma];
int F(int x){
 if(x!=f[x]) f[x]=F(f[x]);
 return f[x];
}
bool cmp2(ANS aa,ANS bb){
 return aa.a<aa.b;
}
void U(int x,int y){
 x=F(x),y=F(y);
 if(x!=y){
   r[x]+=r[y];
   f[y]=x;
 }
}
int ou[ma];
struct Node{
int ed,value;
 Node(){}
 Node(int x,int y):ed(x),value(y){}

};
vector<Node>g[ma];
int z;
int visit[ma];
void  dfs(int id){
  for(int i=0;i<g[id].size();i++){
     Node aa=g[id][i];
     int v=aa.ed;
     int val=aa.value;
     if(visit[v]) continue;
     add(id,v,val);
     visit[v]=1;
     if(!out[v]){
      add(v,sink,max_int);
      z=v;
     }
     dfs(v);
  }
}

int main()
{
  while(cin>>n>>m){
    initial();
    ff(i,1,n)g[i].clear();
    ff(i,1,n){f[i]=i,r[i]=1;}
    fil(in,0);
    fil(out,0);
    ff(i,1,m){
      int a,c,b;
      cin>>a>>b>>c;
      g[a].push_back(Node(b,c));
      out[a]++;
      in[b]++;
      //add(a,b,c);
      //int x=F(a);
      //i//nt y=F(b);
     /// if(x!=y) U(x,y);
    }
    source=0;
    sink=n+1;
    int p=0;
    ff(i,1,n){
      if(!in[i]){
          initial();
          fil(visit,0);
          visit[i]=1;
          add(source,i,max_int);
          z=-1;
          dfs(i);
          if(isap()){
           ans[++p].a=i;
           ans[p].b=z;
           ans[p].c=edge[1].value;
          }
      }
    }

    if(!p){cout<<p<<endl;continue;}
    cout<<p<<endl;
    ff(i,1,p){

     cout<<ans[i].a<<" "<<ans[i].b<<" "<<ans[i].c<<endl;
    }
  }
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#define si "input.txt"
#define so "output.txt"
#define MAX 100000
#define maxn 1000
#define oo 2000000000

using namespace std;

class oNode{
    public:
        long v, c;
        oNode *next;
};

class oEdge{
    public:
        long length, next, start;
        void init(long st){
            length = oo;
            next = -1;
            start = st;
        }
};

typedef oNode *pNode;

void AddNode(pNode a[], long u, long v, long c){
    pNode p = new oNode();
    p->v = v;
    p->c = c;
    p->next = a[u];
    a[u] = p;
}

void input(long &n) { scanf("%ld", &n); }
void input(long &n, pNode a[]){
    input(n);
    for (int i = 0; i < n; i++) {
        long u, v, c;
        input(u); input(v); input(c);
        AddNode(a, u, v, c);
    }
}

void DFS(long u, pNode a[], long x[], oEdge e){
    x[u] = 1;
    pNode p = a[u];
    while (p != NULL){
        e.length = min(e.length, p->c);
        e.next = p->v;
        DFS(p->v, a, x, e);
        p = p->next;
    }
}

void solve(long n, pNode a[], long x[], long aIn[], long aOut[], long &m, oEdge edges[]){
    for (long i = 1; i <= n; i++) x[i] = aIn[i] = aOut[i] = 0;
    for (long i = 1; i <= n; i++){
        pNode p = a[i];
        while (p != NULL){
            aOut[i]++;
            aIn[p->v]++;
            p = p->next;
        }
    }
    for (long i = 1; i <= n; i++){
        if (aIn[i] == 0 && aOut[i] != 0 && x[i] == 0){
            oEdge edge;
            edge.init(i);
            DFS(i, a, x, edge);
            // if (edge.next != -1)
					edges[m++] = edge;
        }
    }
}

void output(long m, oEdge edges[]){
    printf("%ld\n", m);
    for (long i = 0; i < m; i++){
        printf("%ld %ld %ld\n", edges[i].start, edges[i].next, edges[i].length);
    }
}

long n, x[maxn + 1], aIn[maxn + 1], aOut[maxn + 1], m;
pNode a[maxn + 1];
oEdge edges[maxn + 1];

main(){
    // freopen(si, "r", stdin);
    // freopen(so, "w", stdout);
    input(n, a);
    solve(n, a, x, aIn, aOut, m, edges);
    output(m, edges);
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#define si "input.txt"
#define so "output.txt"
#define MAX 100000
#define maxn 1001
#define oo 2000000000

using namespace std;

class oNode{
    public:
        long v, c;
        oNode *next;
};

class oEdge{
    public:
        long length, next, start;
        void init(long st){
            length = oo;
            next = -1;
            start = st;
        }
};

typedef oNode *pNode;

void AddNode(pNode a[], long u, long v, long c){
    pNode p = new oNode();
    p->v = v;
    p->c = c;
    p->next = a[u];
    a[u] = p;
}

void input(long &n) { scanf("%ld", &n); }
void input(long &n, pNode a[]){
    input(n); long temp; input(temp);
    for (int i = 0; i < temp; i++) {
        long u, v, c;
        input(u); input(v); input(c);
        AddNode(a, u, v, c);
    }
}

void DFS(long u, pNode a[], long x[], oEdge &e){
    x[u] = 1;
    pNode p = a[u];
    while (p != NULL){
        e.length = min(e.length, p->c);
        e.next = p->v;
        DFS(p->v, a, x, e);
        p = p->next;
    }
}

void solve(long n, pNode a[], long x[], long aIn[], long aOut[], long &m, oEdge edges[]){
    for (long i = 1; i <= n; i++) x[i] = aIn[i] = aOut[i] = 0;
    for (long i = 1; i <= n; i++){
        pNode p = a[i];
        while (p != NULL){
            aOut[i]++;
            aIn[p->v]++;
            // printf ("%ld %ld\n", i, p->v);
            p = p->next;
        }
    }
    for (long i = 1; i <= n; i++){
        if (aIn[i] == 0 && x[i] == 0){
            // printf("%ld\n", i);
            oEdge edge;
            edge.init(i);
            DFS(i, a, x, edge);
            edges[m++] = edge;
        }
    }
}

void output(long m, oEdge edges[]){
    printf("%ld\n", m);
    for (long i = 0; i < m; i++){
        printf("%ld %ld %ld\n", edges[i].start, edges[i].next, edges[i].length);
    }
}

long n, x[maxn + 1], aIn[maxn + 1], aOut[maxn + 1], m;
pNode a[maxn + 1];
oEdge edges[maxn + 1];

main(){
    // freopen(si, "r", stdin);
    // freopen(so, "w", stdout);
    input(n, a);
    solve(n, a, x, aIn, aOut, m, edges);
    output(m, edges);
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#define si "input.txt"
#define so "output.txt"
#define MAX 100000
#define maxn 1001
#define oo 2000000000

using namespace std;

class oNode{
    public:
        long v, c;
        oNode *next;
};

class oEdge{
    public:
        long length, next, start;
        void init(long st){
            length = oo;
            next = -1;
            start = st;
        }
};

typedef oNode *pNode;

void AddNode(pNode a[], long u, long v, long c){
    pNode p = new oNode();
    p->v = v;
    p->c = c;
    p->next = a[u];
    a[u] = p;
}

void input(long &n) { scanf("%ld", &n); }
void input(long &n, pNode a[]){
    input(n); long temp; input(temp);
    for (int i = 0; i < temp; i++) {
        long u, v, c;
        input(u); input(v); input(c);
        AddNode(a, u, v, c);
    }
}

void DFS(long u, pNode a[], long x[], oEdge &e){
    x[u] = 1;
    pNode p = a[u];
    while (p != NULL){
        e.length = min(e.length, p->c);
        e.next = p->v;
        DFS(p->v, a, x, e);
        p = p->next;
    }
}

void solve(long n, pNode a[], long x[], long aIn[], long aOut[], long &m, oEdge edges[]){
    for (long i = 1; i <= n; i++) x[i] = aIn[i] = aOut[i] = 0;
    for (long i = 1; i <= n; i++){
        pNode p = a[i];
        while (p != NULL){
            aOut[i]++;
            aIn[p->v]++;
            // printf ("%ld %ld\n", i, p->v);
            p = p->next;
        }
    }
    for (long i = 1; i <= n; i++){
        if (aIn[i] == 0 && x[i] == 0){
            // printf("%ld\n", i);
            oEdge edge;
            edge.init(i);
            DFS(i, a, x, edge);
            if (edge.next != -1)
            edges[m++] = edge;
        }
    }
}

void output(long m, oEdge edges[]){
    printf("%ld\n", m);
    for (long i = 0; i < m; i++){
        printf("%ld %ld %ld\n", edges[i].start, edges[i].next, edges[i].length);
    }
}

long n, x[maxn + 1], aIn[maxn + 1], aOut[maxn + 1], m;
pNode a[maxn + 1];
oEdge edges[maxn + 1];

main(){
    // freopen(si, "r", stdin);
    // freopen(so, "w", stdout);
    input(n, a);
    solve(n, a, x, aIn, aOut, m, edges);
    output(m, edges);
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#define si "input.txt"
#define so "output.txt"
#define MAX 100000
#define maxn 1000
#define oo 2000000000

using namespace std;

class oNode{
    public:
        long v, c;
        oNode *next;
};

class oEdge{
    public:
        long length, next, start;
        void init(long st){
            length = oo;
            next = -1;
            start = st;
        }
};

typedef oNode *pNode;

void AddNode(pNode a[], long u, long v, long c){
    pNode p = new oNode();
    p->v = v;
    p->c = c;
    p->next = a[u];
    a[u] = p;
}

void input(long &n) { scanf("%ld", &n); }
void input(long &n, pNode a[]){
    input(n);
    for (int i = 0; i < n; i++) {
        long u, v, c;
        input(u); input(v); input(c);
        AddNode(a, u, v, c);
    }
}

void DFS(long u, pNode a[], long x[], oEdge e){
    x[u] = 1;
    pNode p = a[u];
    while (p != NULL){
        e.length = min(e.length, p->c);
        e.next = p->v;
        DFS(p->v, a, x, e);
        p = p->next;
    }
}

void solve(long n, pNode a[], long x[], long aIn[], long aOut[], long &m, oEdge edges[]){
    for (long i = 1; i <= n; i++) x[i] = aIn[i] = aOut[i] = 0;
    for (long i = 1; i <= n; i++){
        pNode p = a[i];
        while (p != NULL){
            aOut[i]++;
            aIn[p->v]++;
            p = p->next;
        }
    }
    for (long i = 1; i <= n; i++){
        if (aIn[i] == 0 && x[i] == 0){
            oEdge edge;
            edge.init(i);
            DFS(i, a, x, edge);
            if (edge.next != -1) edges[m++] = edge;
        }
    }
}

void output(long m, oEdge edges[]){
    printf("%ld\n", m);
    for (long i = 0; i < m; i++){
        printf("%ld %ld %ld\n", edges[i].start, edges[i].next, edges[i].length);
    }
}

long n, x[maxn + 1], aIn[maxn + 1], aOut[maxn + 1], m;
pNode a[maxn + 1];
oEdge edges[maxn + 1];

main(){
    // freopen(si, "r", stdin);
    // freopen(so, "w", stdout);
    input(n, a);
    solve(n, a, x, aIn, aOut, m, edges);
    output(m, edges);
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class truba
{
    public:
    int t1,t2;
    long d;

    truba(int,int,long);
};

truba::truba(int a1,int a2,long a3)
{
    t1=a1;
    t2=a2;
    d=a3;
}

bool operator < (const truba &a1,const truba &a2)
{
    return a1.t1<a2.t1;
}

int main()
{
#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
#endif
    vector <truba> t;

    int a1[1005],a2[1005];
    int tin[1005],tout[1005];
    long td[1005];

    int n,tcol,i,j,k;

    cin >> n;

    for(i=1;i<=n;i++)
    {
        a1[i]=-1;
        a2[i]=-1;
    }

    cin >> tcol;

    for(i=0;i<tcol;i++)
    {
        cin >> tin[i];
        cin >> tout[i];
        cin >> td[i];

        a2[tin[i]]=i;
        a1[tout[i]]=i;
    }

    long dmin;
    for(i=1;i<=n;i++)
    {
        if(a1[i]==-1 && a2[i]!=-1)
        {
            j=i;
            dmin=-1;
            while(a2[j]!=-1)
            {
                if(dmin==-1 || td[a2[j]]<dmin){dmin=td[a2[j]];}
                j=tout[a2[j]];
            }
            t.push_back(truba(i,j,dmin));
        }
    }

    sort(t.begin(),t.end());
    cout << t.size() << "\n";
    for(i=0;i<t.size();i++)
    {
        cout << t[i].t1 << " " << t[i].t2 << " " << t[i].d << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class truba
{
    public:
    int t1,t2;
    long d;

    truba(int,int,long);
};

truba::truba(int a1,int a2,long a3)
{
    t1=a1;
    t2=a2;
    d=a3;
}

bool operator < (const truba &a1,const truba &a2)
{
    return a1.d<a2.d;
}

int main()
{
#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
#endif
    vector <truba> t;

    int a1[1005],a2[1005];
    int tin[1005],tout[1005];
    long td[1005];

    int n,tcol,i,j,k;

    cin >> n;

    for(i=1;i<=n;i++)
    {
        a1[i]=-1;
        a2[i]=-1;
    }

    cin >> tcol;

    for(i=0;i<tcol;i++)
    {
        cin >> tin[i];
        cin >> tout[i];
        cin >> td[i];

        a2[tin[i]]=i;
        a1[tout[i]]=i;
    }

    long dmin;
    for(i=1;i<=n;i++)
    {
        if(a1[i]==-1 && a2[i]!=-1)
        {
            j=i;
            dmin=-1;
            while(a2[j]!=-1)
            {
                if(dmin==-1 || td[a2[j]]<dmin){dmin=td[a2[j]];}
                j=tout[a2[j]];
            }
            t.push_back(truba(i,j,dmin));
        }
    }

    sort(t.begin(),t.end());
    cout << t.size() << "\n";
    for(i=t.size()-1;i>=0;i--)
    {
        cout << t[i].t1 << " " << t[i].t2 << " " << t[i].d << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;

class truba
{
    public:
    int t1,t2;
    long d;

    truba(int,int,long);
};

truba::truba(int a1,int a2,long a3)
{
    t1=a1;
    t2=a2;
    d=a3;
}

bool operator < (const truba &a1,const truba &a2)
{
    return a1.d<a2.d;
}

int main()
{
#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
#endif
    vector <truba> t;

    int a1[1005],a2[1005];
    int tin[1005],tout[1005];
    long td[1005];

    int n,tcol,i,j,k;

    cin >> n;

    for(i=1;i<=n;i++)
    {
        a1[i]=-1;
        a2[i]=-1;
    }

    cin >> tcol;

    for(i=0;i<tcol;i++)
    {
        cin >> tin[i];
        cin >> tout[i];
        cin >> td[i];

        a2[tin[i]]=i;
        a1[tout[i]]=i;
    }

    long dmin;
    for(i=1;i<=n;i++)
    {
        if(a1[i]==-1 && a2[i]!=-1)
        {
            j=i;
            dmin=-1;
            while(a2[j]!=-1)
            {
                if(dmin==-1 || td[a2[j]]<dmin){dmin=td[a2[j]];}
                j=tout[a2[j]];
            }
            t.push_back(truba(i,j,dmin));
        }
    }

    sort(t.begin(),t.end());
    cout << t.size() << "\n";
    for(i=t.size()-1;i>=0;i--)
    {
        cout << t[i].t1 << " " << t[i].t2 << " " << t[i].d << "\n";
    }
    return 0;
}


#include <iostream>
#include <limits.h>

using namespace std;

struct s {
	bool l ;
	int b ;
	int d ;
} ;

int main()
{
	int n,i,start,end,min;
	int p,j,k;
	int c = 0;
	//int l[1001] = {0};
	//int h[1001] = {0};
	s ar[1001] = {0};
	//s ans[1001] = {0};
	cin >> n >> p;
	for(i = 0; i < p; i++) {
		cin >> j;
	       	cin >> ar[j].b >> ar[j].d;
		ar[j].l = 1;
	}
	for(i = 1; i <= 1000; i++) {
		if(ar[i].l) {
			min = ar[i].d;
			start = i;
			end = ar[i].b;
	//		cout << start << " " << end << endl;
			while(ar[end].l && ar[end].b != start)
			{
				min = min < ar[end].d ? min : ar[end].d;
				ar[end].l = 0;
				end = ar[end].b;
				k = end;
	//			cout << "in loop "<< min << " "  << end << endl;
			}
			if(ar[end].b == start) {
				ar[start].l = 0;
				ar[k].l = 0;
			}
			else {
				ar[start].l = 1;
				ar[start].d = min;
				ar[start].b = end;
			}
		}
	}
	for(i = 1; i <= 1000; i++)
		if(ar[i].l)
			c++;
	cout << c << endl;
	for(i = 1; i <= 1000; i++)
		if(ar[i].l)
			cout << i << " " << ar[i].b << " " << ar[i].d << endl;

	return 0;
}


#include <iostream>
#include <limits.h>

using namespace std;

struct s {
	bool l ;
	int b ;
	int d ;
} ;

int main()
{
	int n,i,start,end,min;
	int p,j,k;
	int c = 0;
	int l[1001] = {0};
	s ar[1001] = {0};
	s ans[1001] = {0};
	cin >> n >> p;
	for(i = 0; i < p; i++) {
		cin >> j;
	       	cin >> ar[j].b >> ar[j].d;
		ar[j].l = 1;
	}
	for(i = 1; i <= 1000; i++) {
		if(ar[i].l) {
			min = ar[i].d;
			start = i;
			end = ar[i].b;
	//		cout << start << " " << end << endl;
			while(ar[end].l && ar[end].b != start)
			{
				min = min < ar[end].d ? min : ar[end].d;
				ar[end].l = 0;
				end = ar[end].b;
				k = end;
	//			cout << "in loop "<< min << " "  << end << endl;
			}
			if(ar[end].b == start) {
				ar[start].l = 0;
				ar[k].l = 0;
			}
			else {
				c++;
				ans[start].l = 1;
				ans[start].d = min;
				ans[start].b = end;
			}
		}
	}
	cout << c << endl;
	for(i = 1; i <= 1000; i++)
		if(ans[i].l)
			cout << i << " " << ans[i].b << " " << ans[i].d << endl;

	return 0;
}


#include <iostream>
#include <limits.h>

using namespace std;

struct s {
	bool l ;
	int b ;
	int d ;
} ;

int main()
{
	int n,i,start,end,min;
	int p,j;
	int c = 0;
	int l[1001] = {0};
	s ar[1001] = {0};
	s ans[1001] = {0};
	cin >> n >> p;
	for(i = 0; i < p; i++) {
		cin >> j;
	       	cin >> ar[j].b >> ar[j].d;
		ar[j].l = 1;
	}
	for(i = 1; i <= 1000; i++) {
		if(ar[i].l) {
			min = ar[i].d;
			start = i;
			end = ar[i].b;
		//	cout << start << " " << end << endl;
			while(ar[end].l && ar[end].b != start)
			{
				min = min < ar[end].d ? min : ar[end].d;
				ar[end].l = 0;
				end = ar[end].b;
				ar[end].l = 0;
		//		cout << "in loop "<< min << " "  << end << endl;
			}
			if(ar[end].b == start) {
				ar[start].l = 0;
			}
			else {
				c++;
				ans[start].l = 1;
				ans[start].d = min;
				ans[start].b = end;
			}
		}
	}
	cout << c << endl;
	for(i = 1; i <= 1000; i++)
		if(ans[i].l)
			cout << i << " " << ans[i].b << " " << ans[i].d << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=5000;
ll d[N][N];
vector<ll> g[N];
vector<ll> out(N,0);
pair<ll,ll> dfs(ll u){
    if(!g[u].size()){
        return {INT32_MAX,u};
    }
    pair<ll,ll> p=dfs(g[u][0]);
    return make_pair(min(d[u][g[u][0]],p.first),p.second);
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n,p;
    cin>>n>>p;

    for(ll i=0;i<p;i++){
        ll u,v,dist;
        cin>>u>>v>>dist;
        d[u][v]=dist;
        out[u]++;out[v]--;
        g[u].push_back(v);
    }
    vector<tuple<ll,ll,ll>> ans;
    for(ll i=1;i<=n;i++){
        if(out[i]==1){
            pair<ll,ll> p=dfs(i);
            ans.push_back(make_tuple(i,p.second,p.first));

        }
    }
    cout<<ans.size()<<"\n";
    for(tuple<ll,ll,ll> p:ans){
        cout<<get<0>(p)<<" "<<get<1>(p)<<" "<<get<2>(p)<<"\n";
    }
    return 0;
}


/*
0687B. Remainders Game
*/

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

int main(){
  long int n, k, c, d = 1;
  cin >> n >> k;
  bool flag = false;
  map<long int, int> occ;
  while(n--){
    cin >> c;
    d = d*c/__gcd(c,d);
    if(d%k == 0){cout << "Yes"; flag = true; break;}
  }
  if(flag == false){cout << "No";}
}


/*
0687B. Remainders Game
*/

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

long int gcd(long int x,long int y){
  long int a = max(x,y), b = min(x,y), c;
  while(a%b != 0){
    a -= (a/b)*b;
    c = a; a = b; b = c;
  }
  return b;
}

int main(){
  long int n, k, c;
  cin >> n >> k;
  bool flag = false;
  map<long int, int> occ;
  while(n--){
    cin >> c;
    if(occ[c] == 1){continue;}
    else{occ[c]++;}
    if(c%k == 0){
      cout << "Yes";
      flag = true;
      break;
    }
    else if(gcd(gcd(k,c),k/gcd(k,c)) == 1){k/=gcd(k,c);}
  }

  if(flag == false){cout << "No";}
}


/*
0687B. Remainders Game
*/

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

int main(){
  long long int n, k, c, d = 1;
  cin >> n >> k;
  bool flag = false;
  while(n--){
    cin >> c;
    d = (d*c)/__gcd(c,d);
    if(d%k == 0){cout << "Yes"; flag = true; break;}
  }
  if(flag == false){cout << "No";}
}


/*
0687B. Remainders Game
0688D. Remainders Game
*/

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

int main(){
  long long int n, k, c, d = 1;
  cin >> n >> k;
  while(n--){
    cin >> c;
    d = (d*c)/__gcd(c,d);
    d = __gcd(k,d);
  }
  cout << ((d == k)? "Yes": "No");
}


/*
0687B. Remainders Game
*/

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

int main(){
  long long int n, k, c, d = 1;
  cin >> n >> k;
  bool flag = false;
  while(n--){
    cin >> c;
    d = (d*c)/__gcd(c,d);
    d = __gcd(k,d);
    if(d == k){cout << "Yes"; flag = true; break;}
  }
  if(flag == false){cout << "No";}
}


/*
0687B. Remainders Game
0688D. Remainders Game
*/

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

int main(){
  long long int n, k, c, d = 1;
  cin >> n >> k;
  while(n--){
    scanf("%lld", &c);
    d = (d*c)/__gcd(c,d);
    d = __gcd(k,d);
  }
  cout << ((d == k)? "Yes": "No");
}


/*
0687B. Remainders Game
0688D. Remainders Game
*/

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

int main(){
  long long int n, k, c, d = 1;
  cin >> n >> k;
  while(n--){
    scan("%lld", &c);
    d = (d*c)/__gcd(c,d);
    d = __gcd(k,d);
  }
  cout << ((d == k)? "Yes": "No");
}


/*
0687B. Remainders Game
*/

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

long int gcd(long int x,long int y){
  long int a = max(x,y), b = min(x,y), c;
  while(a%b != 0){
    a -= (a/b)*b;
    c = a; a = b; b = c;
  }
  return b;
}

int main(){
  long int n, k, c;
  cin >> n >> k;
  bool flag = false;
  map<long int, int> occ;
  while(n--){
    cin >> c;
    if(occ[c] == 1){continue;}
    else{occ[c]++;}
    if(c%k == 0){
      cout << "Yes";
      flag = true;
      break;
    }
    else{
      long int d = gcd(k,c);
      if(gcd(d,k/d) == 1){k /= d;}
    }
  }

  if(flag == false){cout << "No";}
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
bool c   [1000001];
int prime[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int res=1;
        while(n%i==0)
        {
            n=n/i;
            res*=i;
        }
        if(!c[res]) return false;
    }

}
if(n>1) if(!c[n]) return false;
return true;
}
void seive(int c1)
{
    while(c1>1)
    {
        if(prime[c1]==-1)
        {
            c[c1]=true;
            c1/=c1;
        }
        else
        {
            c[prime[c1]]=true;
            c1/=prime[c1];
        }
    }
}

int main()
{
int n,k,c1;
cin>>n>>k;
for(int i=0;i<1000001;i++) c[i]=false;
for(int i=0;i<1000001;i++) prime[i]=-1;

for(int i=2;i<1001;i++)
{
    if(prime[i]==-1)
    {
        for(int j=i*i;j<1000001;j+=i) if(prime[j]==-1) prime[j]=i;
    }
}
for(int i=0;i<n;i++)
{
  cin>>c1;
  c[c1]=true;
  seive(c1);
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
bool c   [1000001];
int prime[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int res=1;
        while(n%i==0)
        {
            n=n/i;
            res*=i;
        }
        if(!c[res]) return false;
    }

}
if(n>1) if(!c[n]) return false;
return true;
}
void seive(int c1)
{
    while(c1>1)
    {
        if(prime[c1]==-1)
        {
            c[c1]=true;
            c1/=c1;
        }
        else
        {
            c[prime[c1]]=true;
            c1/=prime[c1];
        }
    }
}

int main()
{
int n,k,c1;
cin>>n>>k;
for(int i=0;i<1000001;i++) c[i]=false;
for(int i=0;i<1000001;i++) prime[i]=-1;

for(int i=2;i<1001;i++)
{
    if(prime[i]==-1)
    {
        for(int j=i*i;j<1000001;j+=i) if(prime[j]==-1) prime[j]=i;
    }
}
for(int i=0;i<n;i++)
{
  cin>>c1;
  if(!c[c1])
 {c[c1]=true;
  seive(c1);}
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
int c   [1000001];
int prime[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int count=0;
        while(n%i==0)
        {
            n=n/i;
            count++;
        }
        if(count>c[i]) return false;
    }

}
if(n>1) if(c[n]<1) return false;
return true;
}
void seive(int c1)
{   int prev=-1;
    int counter=0;
    while(c1>1)

    {
       if(prime[c1]!=prev)
       {
           if(prev==-1) prev=prime[c1];
           else
           {
               c[prev]=max(counter,c[prev]);
               counter=0;
               prev=prime[c1];
           }
       }
       else
       {
           counter++;
           c1=c1/prime[c1];
           if(c1==1)
           {
               c[prev]=max(counter,c[prev]);
               return;
           }
       }
    }
}

int main()
{
int n,k,c1;
//cin>>n>>k;
scanf("%d%d",&n,&k);
for(int i=0;i<1000001;i++) c[i]=0;
for(int i=0;i<1000001;i++) prime[i]=-1;

for(int i=2;i<1001;i++)
{
    if(prime[i]==-1)
    {
        for(int j=i*i;j<1000001;j+=i) if(prime[j]==-1) prime[j]=i;
    } }

for(int i=2;i<1000001;i++) if(prime[i]==-1) prime[i]=i;
for(int i=0;i<n;i++)
{
  //cin>>c1;
  scanf("%d",&c1);

 {//c[c1]+=1;
  seive(c1);}
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
bool c   [1000001];
int prime[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int res=1;
        while(n%i==0)
        {
            n=n/i;
            res*=i;
        }
        if(!c[res]) return false;
    }

}
if(n>1) if(!c[n]) return false;
return true;
}
void seive(int c1)
{
    while(c1>1)
    {
        if(prime[c1]==-1)
        {
            c[c1]=true;
            c1/=c1;
        }
        else
        {
            c[prime[c1]]=true;
            c1/=prime[c1];
        }
    }
}

int main()
{
int n,k,c1;
//cin>>n>>k;
scanf("%d%d",&n,&k);
for(int i=0;i<1000001;i++) c[i]=false;
for(int i=0;i<1000001;i++) prime[i]=-1;

for(int i=2;i<1001;i++)
{
    if(prime[i]==-1)
    {
        for(int j=i*i;j<1000001;j+=i) if(prime[j]==-1) prime[j]=i;
    }
}
for(int i=0;i<n;i++)
{
  //cin>>c1;
  scanf("%d",&c1);
  if(!c[c1])
 {c[c1]=true;
  seive(c1);}
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
bool c[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int res=1;
        while(n%i==0)
        {
            n=n/i;
            res*=i;
        }
        if(!c[res]) return false;
    }
}
if(n>1) if(!c[n]) return false;
return true;
}
int main()
{
int n,k,c1;
cin>>n>>k;
for(int i=0;i<1000001;i++) c[i]=false;
for(int i=0;i<n;i++)
{
  cin>>c1;
  c[c1]=true;
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
bool c   [1000001];
int prime[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int res=1;
        while(n%i==0)
        {
            n=n/i;
            res*=i;
        }
        if(!c[res]) return false;
    }

}
if(n>1) if(!c[n]) return false;
return true;
}
void seive(int c1)
{
    while(c1>1)
    {
        if(prime[c1]==-1)
        {
            c[c1]=true;
            c1/=c1;
        }
        else
        {
            c[prime[c1]]=true;
            c1/=prime[c1];
        }
    }
}

int main()
{
int n,k,c1;
cin>>n>>k;
for(int i=0;i<1000001;i++) c[i]=false;
for(int i=0;i<1000001;i++) prime[i]=-1;

for(int i=2;i<1000001;i++)
{
    if(prime[i]==-1)
    {
        for(int j=i*i;j<1000001;j+=i) if(prime[j]==-1) prime[j]=i;
    }
}
for(int i=0;i<n;i++)
{
  cin>>c1;
  c[c1]=true;
  seive(c1);
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
#include <bits/stdc++.h>
#include <list>
#include<stdio.h>
#define ll long long
using namespace std;
int c   [1000001];
int prime[1000001];
bool ans(int n)
{
    for(int i=2;i*i<=n;i++)
{
    if(n%i==0)
    {
        int count=0;
        while(n%i==0)
        {
            n=n/i;
            count++;
        }
        if(count>c[i]) return false;
    }

}
if(n>1) if(c[n]<1) return false;
return true;
}
void seive(int c1)
{
    while(c1>1)
    {
        if(prime[c1]==-1)
        {
            c[c1]+=1;
            c1/=c1;
        }
        else
        {
            c[prime[c1]]+=1;
            //c[c1/prime[c1]]=true;
            c1/=prime[c1];
        }
    }
}

int main()
{
int n,k,c1;
//cin>>n>>k;
scanf("%d%d",&n,&k);
for(int i=0;i<1000001;i++) c[i]=0;
for(int i=0;i<1000001;i++) prime[i]=-1;

for(int i=2;i<1001;i++)
{
    if(prime[i]==-1)
    {
        for(int j=i*i;j<1000001;j+=i) if(prime[j]==-1) prime[j]=i;
    }
}
for(int i=0;i<n;i++)
{
  //cin>>c1;
  scanf("%d",&c1);

 {//c[c1]+=1;
  seive(c1);}
}
bool a=ans(k);
if(a) cout<<"Yes";
else cout<<"No";
return 0;
}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
int cnt[N];
int c[N];
int n, k;
vector<int> v;

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = c[i]; j < N; j += c[i])
		{
			cnt[j]++;
		}
	}
	for (int i = 0; i < N; i++)
	{
		if (cnt[i] == n)
		{
			if (!v.size())
			{
				v.push_back(i % k);
			}
			else
			{
				if (v[0] != i % k)
				{
					cout << "No" << endl;
					exit(0);
				}
			}
		}
	}
	if (!v.size())
	{
		cout << "No" << endl;
	}
	cout << "Yes" << endl;
}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
long long ans = 1;
int c[N];
int n, k;

int main()
{
     std::ios::sync_with_stdio(false);
     cin.tie(0);
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
		ans = lcm(ans, c[i]);
		ans = ans % k;
	}
	if (ans % k == 0)
	{
	    cout << "Yes" << endl;
	}
	else
	{
	    cout << "No" << endl;
	}

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
int ans = 1;
int c[N];
int n, k;

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
		ans = lcm(ans, c[i]);
	}
	if (ans >= k)
	{
	    cout << "Yes" << endl;
	}
	else
	{
	    cout << "No" << endl;
	}

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
int cnt[N];
int c[N];
int n, k;
vector<int> v;

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = c[i]; j < N; j += c[i])
		{
			cnt[j]++;
		}
	}
	for (int i = 0; i < N; i++)
	{
		if (cnt[i] == n)
		{
			if (!v.size())
			{
				v.push_back(i % k);
			}
			else
			{
				if (v[0] != i % k)
				{
					cout << "No" << endl;
					exit(0);
				}
			}
		}
	}
	if (!v.size())
	{
		cout << "No" << endl;
		exit(0);
	}
	cout << "Yes" << endl;
}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
long long ans = 1;
int c[N];
int n, k;

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
		ans = lcm(ans, c[i]);
		ans = ans % k;
	}
	if (ans % k == 0)
	{
	    cout << "Yes" << endl;
	}
	else
	{
	    cout << "No" << endl;
	}

}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
int cnt[N];
int c[N];
int n, k;
vector<int> v;

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = c[i]; j < N; j += c[i])
		{
			cnt[j]++;
		}
	}
	for (int i = 0; i < N; i++)
	{
		if (cnt[i] == n)
		{
			if (!v.size())
			{
				v.push_back(i % k);
			}
			else
			{
				if (v[0] != i % k)
				{
					cout << "No" << endl;
					exit(0);
				}
			}
		}
	}
	cout << "Yes" << endl;
}


//Be Name Khoda, Besmealong longah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const int N = 1e6 + 5;
int ans = 1;
int c[N];
int n, k;

int main()
{
    std::ios::sync_with_stdio(0);
    cin.tie(0);
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> c[i];
		ans = lcm(ans, c[i]);
		ans = ans % k;
	}
	if (ans % k == 0)
	{
	    cout << "Yes" << endl;
	}
	else
	{
	    cout << "No" << endl;
	}

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , x , ans = 0;
int main(){
fcin;
    cin >> n >> k;
    for(int i = 0 ; i  < n ; i++){
        cin >> x;
        if(x%k==0) ans = 1;
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 1000001
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , a[N] , have[N] , pr[N] , ans = 1;
int main(){
fcin;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i++){
        int x ;
        cin >> x;
        a[x] = 1;
    }
    for(int i = 2 ; i*i <= 10e5 ; i++){
        if(pr[i]==0 && a[i]==1) have[i] = 1;
        for(int j = 2 ; j*i <= 10e5 ; j++){
            pr[i*j] = 1;
            if(a[i*j]==1){
                if(pr[i]==0) have[i] = 1;
            }
        }
    }
    // cout << have[5] << "\n";
    for(int i = 2 ; i <= k ; i++){
        if(k%i==0 && pr[i]==0){
            if(have[i]==0) ans = 0;
        }
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 30000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , a[N] , have[N] , pr[N] , ans = 1;
int main(){
fcin;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i++){
        int x ;
        cin >> x;
        a[x] = 1;
    }
    for(int i = 2 ; i*i <= 10e6 ; i++){
        if(pr[i]==0 && a[i]==1) have[i] = 1;
        for(int j = 2 ; j*i <= 10e6 ; j++){
            pr[i*j] = 1;
            if(a[i*j]==1){
                if(pr[i]==0) have[i] = 1;
            }
        }
    }
    // cout << have[5] << "\n";
    for(int i = 2 ; i <= k ; i++){
        if(k%i==0 && pr[i]==0){
            if(have[i]==0) ans = 0;
        }
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 10000001
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , a[N] , have[N] , pr[N] , ans = 1;
int main(){
fcin;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i++){
        int x ;
        cin >> x;
        a[x] = 1;
    }
    for(int i = 2 ; i*i <= 10e6 ; i++){
        if(pr[i]==0 && a[i]==1) have[i] = 1;
        for(int j = 2 ; j*i <= 10e6 ; j++){
            pr[i*j] = 1;
            if(a[i*j]==1){
                if(pr[i]==0) have[i] = 1;
            }
        }
    }
    // cout << have[5] << "\n";
    for(int i = 2 ; i <= k ; i++){
        if(k%i==0 && pr[i]==0){
            if(have[i]==0) ans = 0;
        }
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , x , ans = 0;
int main(){
fcin;
	cin >> n >> k;
	for(int i = 0 ; i  < n ; i++){
		cin >> x;
		if(x>=k) ans = 1;
	}
	if(ans) cout << "Yes\n";
	else cout << "No\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 1000001
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , a[N] , have[N] , pr[N] , c[N] , ans = 1;
int main(){
fcin;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i++){
        int x ;
        cin >> x;
        a[x] = 1;
    }
    for(int i = 2 ; i*i <= 10e5 ; i++){
        for(int j = 2 ; j*i <= 10e5 ; j++){
            int x = i*j;
            pr[x] = 1;
            if(a[x] && !pr[i]){
                int f = 0;
                while(x>1 && x%i==0) {
                    x/=i;
                    f++;
                }
                c[i] = max(c[i],f);
            }
        }
    }
    for(int i = 1 ; i<= 10e5 ; i++){
        if(!pr[i] && a[i]) c[i] = 1;
    }
    int kk = k;
    for(int i = 2 ; i <= kk ; i++){
        int f = 0;
        while(k>1 && k%i==0){
            k/=i;
            f++;
        }
        if(c[i] < f) ans = 0;
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 1000001
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , k , a[N] , have[N] , pr[N] , ans = 1;
int main(){
fcin;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i++){
        int x ;
        cin >> x;
        a[x] = 1;
    }
    for(int i = 2 ; i*i <= 10e5 ; i++){
        for(int j = 2 ; j*i <= 10e5 ; j++){
            pr[i*j] = 1;
            if(a[i*j]==1){
                if(pr[i]==0) have[i] = 1;
            }
        }
    }
    for(int i = 1 ; i <= 10e5 ; i++){
      if(pr[i]==0 && a[i]==1) have[i] = 1;
    }
    for(int i = 2 ; i <= k ; i++){
        if(k%i==0 && pr[i]==0){
            if(have[i]==0) ans = 0;
        }
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}


#include<bits/stdc++.h>
using namespace std;
int n;
long long arr[1000007],cnt[1000007],vis[1000007],k;
int main()
{
    scanf("%d%lld",&n,&k);
    long long x=1;
    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        arr[x]++;
    }

    for(int i=2;i<=sqrt(1000000);i++){
        if(vis[i]==0){
            for(int j=i;j<=1000000;j+=i){
                vis[j]=1;
       // if(j==5) cout<<arr[j]<<endl;
                if(arr[j]){
                    int x=j;
                    int c=0;
                    while(x%i==0){
                        c++;
                        x/=i;
                    }
                    cnt[i]=max(cnt[i],c*1ll);
                  //  cout<<cnt[i]<<' '<<i<<endl;
                }
            }
        }
    }
    long long ans=1;
    for(long long i=1;i<=1000000;i++){
        while(cnt[i]--){
            ans=(ans*i)%k;
           // cout<<ans<<' '<<i<<endl;
        }
    }
    if(ans) printf("No\n");
    else printf("Yes\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
long long arr[1000007],cnt[1000007],vis[1000007],k;
int main()
{
    scanf("%d%lld",&n,&k);
    long long x=1;
    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        arr[x]++;
    }

    for(int i=2;i<=1000000;i++){
        if(vis[i]==0){
            for(int j=i;j<=1000000;j+=i){
                vis[j]=1;
       // if(j==5) cout<<arr[j]<<endl;
                if(arr[j]){
                    int x=j;
                    int c=0;
                    while(x%i==0){
                        c++;
                        x/=i;
                    }
                    cnt[i]=max(cnt[i],c*1ll);
                  //  cout<<cnt[i]<<' '<<i<<endl;
                }
            }
        }
    }
    long long ans=1;
    for(long long i=1;i<=1000000;i++){
        while(cnt[i]--){
            ans=(ans*i)%k;
           // cout<<ans<<' '<<i<<endl;
        }
    }
    if(ans%k) printf("No\n");
    else printf("Yes\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
long long arr[1000007],k;
int main()
{
    cin>>n>>k;
    long long x=1;
    for(int i=0;i<n;i++){
        cin>>arr[i];
        x*=arr[i];
        if(x%k==0){
            cout<<"Yes"<<endl;
            return 0;
        }
        x%=k;
    }
    cout<<"No"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
long long arr[1000007],k;
int main()
{
    cin>>n>>k;
    long long x=1;
    for(int i=0;i<n;i++){
        cin>>arr[i];
        x*=arr[i];
        if(x%k==0){
            cout<<"Yes"<<endl;
            return 0;
        }
    }
    cout<<"No"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
long long arr[1000007],k;
int main()
{
    cin>>n>>k;
    long long x=1;
    for(int i=0;i<n;i++){
        cin>>arr[i];
        x*=arr[i];
        if(x%k==0){
            cout<<"Yes"<<endl;
        }
    }
    cout<<"No"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
long long arr[1000007],cnt[1000007],vis[1000007],k;
int main()
{
    scanf("%d%lld",&n,&k);
    long long x=1;
    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        arr[x]++;
    }

    for(int i=2;i<=1000000;i++){
        if(vis[i]==0){
            for(int j=i;j<=1000000;j+=i){
                vis[j]=1;
       // if(j==5) cout<<arr[j]<<endl;
                if(arr[j]){
                    int x=j;
                    int c=0;
                    while(x%i==0){
                        c++;
                        x/=i;
                    }
                    cnt[i]=max(cnt[i],c*1ll);
                  //  cout<<cnt[i]<<' '<<i<<endl;
                }
            }
        }
    }
    long long ans=1;
    for(long long i=1;i<=1000000;i++){
        while(cnt[i]--){
            ans=(ans*i)%k;
           // cout<<ans<<' '<<i<<endl;
        }
    }
    if(ans) printf("No\n");
    else printf("Yes\n");
    return 0;
}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))

using namespace std;

int pgcd(int x, int y) { return __gcd(x, y); }
long long ppccm(int x, int y) { return x * 1ll * y / gcd(x, y); }

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int countm = 1;
    for (int i = 0; i < n; ++i) {
        int tab;
        cin >> tab;
        countm = pgcd(k, lcm(countm, tab));
    }
    if (countm == k) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))

using namespace std;

int pgcd(int x, int y) { return __gcd(x, y); }
long long ppccm(int x, int y) { return x * 1ll * y / gcd(x, y); }

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int countm = 1;
    for (int i = 0; i < n; ++i) {
        int tab;
        cin >> tab;
        countm = __gcd(k, lcm(countm, tab));
    }
    if (countm == k) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))
using namespace std;
typedef pair<long long,long long> ii ;

int main()
{ long long n,k,countm=1;
cin>>n>>k;
//cout<<lcm(10,20);

for(long long i=0;i<n;i++)
{
    long long tab;
cin>>tab;
countm=gcd(k,lcm(countm,tab));

}

if(countm==k)
cout<<"Yes";
else
{
    cout<<"No";
}

    return 0;

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))
using namespace std;
typedef pair<long long,long long> ii ;

int main()
{ long long n,k,countm=1;
cin>>n>>k;
//cout<<lcm(10,20);
long long tab;

for(long long i=0;i<n;i++)
{
cin>>tab;
countm=gcd(k,lcm(countm,tab));

}

if(__gcd(k,countm)==k)
cout<<"Yes";
else
{
    cout<<"No";
}

    return 0;

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))

using namespace std;

int pgcd(int x, int y) { return __gcd(x, y); }
long long ppccm(int x, int y) { return x * 1ll * y / gcd(x, y); }

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int countm = 1;
    for (int i = 0; i < n; ++i) {
        int tab;
        cin >> tab;
        countm = pgcd(k, lcm(countm, tab));
    }
    if (countm == k) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

}
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define int long long
const int N = 0, mod = 0;

int gcd(int x, int y) { return __gcd(x, y); }
long long lcm(int x, int y) { return x * 1ll * y / gcd(x, y); }

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int cur = 1;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        cur = gcd(k, lcm(cur, x));
    }
    if (cur == k) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))
using namespace std;
typedef pair<long long,long long> ii ;

int main()
{  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    long long n,k,countm=1;
cin>>n>>k;
//cout<<lcm(10,20);

for(long long i=0;i<n;i++)
{
    long long tab;
cin>>tab;
countm=gcd(k,lcm(countm,tab));

}

if(countm==k)
cout<<"Yes";
else
{
    cout<<"No";
}

    return 0;

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))

typedef pair<long long,long long> ii ;

using namespace std;

int pgcd(int x, int y) { return __gcd(x, y); }
long long ppccm(int x, int y) { return x * 1ll * y / gcd(x, y); }

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int countm = 1;
    for (int i = 0; i < n; ++i) {
        int tab;
        cin >> tab;
        countm = __gcd(k, lcm(countm, tab));
    }
    if (countm == k) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))
using namespace std;
typedef pair<long long,long long> ii ;

int main()
{ long long n,k,countm=1;
cin>>n>>k;
//cout<<lcm(10,20);
long long tab;

for(int i=0;i<n;i++)
{
cin>>tab;
countm=gcd(k,lcm(countm,tab));

}

if(__gcd(k,countm)==k)
cout<<"Yes";
else
{
    cout<<"No";
}

    return 0;

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))
using namespace std;
typedef pair<long long,long long> ii ;

int main()
{ long long n,k,countm=1;
cin>>n>>k;
//cout<<lcm(10,20);
long long tab;

for(int i=0;i<n;i++)
{
cin>>tab;
countm=lcm(countm,tab);

}

if(__gcd(k,countm)==k)
cout<<"Yes";
else
{
    cout<<"No";
}

    return 0;

}


#include <iostream>
#include<vector>
#include<cmath>
#include <set>

#include<bits/stdc++.h>

#include <iostream>
#include <cmath>
#include <numeric>

#define ABS(a) ((a>0)?a:-(a))

typedef pair<long long,long long> ii ;

using namespace std;

int pgcd(int x, int y) { return __gcd(x, y); }
long long ppccm(int x, int y) { return x * 1ll * y / gcd(x, y); }

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int countm = 1;
    for (int i = 0; i < n; ++i) {
        int tab;
        cin >> tab;
        countm = pgcd(k, lcm(countm, tab));
    }
    if (countm == k) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  return (a * b) / gcd(a, b);
}

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

  ll n, k;
  cin >> n >> k;
  ll mmc = 1;
  while (n--) {
    ll a;
    cin >> a;

    mmc = lcm(a, mmc);
    mmc = gcd(mmc, k);
  }
  cout << (gcd(mmc, k) == k ? "Yes" : "No") << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  return b == 0 ? a : gcd(a % b, b);
}

ll lcm(ll a, ll b) {
  return (a * b) / gcd(a, b);
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll mmc = 1;
  while (n--) {
    ll a;
    cin >> a;

    mmc = lcm(__gcd(k, a), mmc);
    mmc = gcd(mmc, k);
  }
  cout << (gcd(mmc, k) == k ? "Yes" : "No") << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll lcm(ll a, ll b) {
  return (a * b) / __gcd(a, b);
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll mmc = 1;
  while (n--) {
    ll a;
    cin >> a;

    mmc = lcm(__gcd(k, a), mmc);
  }
  cout << (__gcd(mmc, k) == k ? "Yes" : "No") << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  return (a * b) / gcd(a, b);
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll mmc = 1;
  while (n--) {
    ll a;
    cin >> a;

    mmc = lcm(a, mmc);
    mmc = gcd(mmc, k);
  }
  cout << (gcd(mmc, k) == k ? "Yes" : "No") << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  return __gcd(a, b);
  return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  return (a * b) / gcd(a, b);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  ll mmc = 1;
  while (n--) {
    ll a;
    cin >> a;

    mmc = lcm(a, mmc);
    mmc = gcd(mmc, k);
  }
  cout << (gcd(mmc, k) == k ? "Yes" : "No") << endl;
}