#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define maxn 10001
using namespace std;
vector<int> G[maxn];
int BeFok[maxn];
int van_szulo[maxn],van_gyerek[maxn],szulo[maxn][2];
int n;
// mintafeladat megoldását használva
// él: gyerek felé mutat
void Beolvas(){
int m,p,q1,q2;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){BeFok[i]=0;van_szulo[i]=0;van_gyerek[i]=0;}
for (int i=0;i<m;i++){
scanf("%d%d%d",&p,&q1,&q2);
G[q1].push_back(p);
G[q2].push_back(p);
BeFok[p]+=2;
szulo[p][0]=q1;
szulo[p][1]=q2;
van_szulo[p]=1;
van_gyerek[q1]=1;
van_gyerek[q2]=1;
}
}
int main(){
Beolvas();
vector<int> nev[n];
int cnt,d,i,j,p,q,si,si0,si1,si2,size,szulo1,szulo2,pos,pos1,pos2,melyseg=0,inv[maxn+1],sol[maxn],meret[maxn+1];
unsigned int bit[32],**os,ans[maxn+1];
// topologikus sorrend megkeresése
for (int p=1;p<=n;p++)
if(BeFok[p]==0)
nev[0].push_back(p);
while(nev[melyseg].size()>0){
si=nev[melyseg].size();
for(i=0;i<si;i++){
p=nev[melyseg][i];
si2=G[p].size();
for(j=0;j<si2;j++){
q=G[p][j];
BeFok[q]--;
if(BeFok[q]==0)
nev[melyseg+1].push_back(q);
}
}
melyseg++;
}
bit[0]=1;for(i=1;i<32;i++)bit[i]=2*bit[i-1];
si=(n+32)/32;
os=(unsigned int**)malloc((n+1)*sizeof(unsigned int*));
//for(i=0;i<=n;i++)os[i]=(unsigned int*)malloc(si*sizeof(unsigned int));
pos=1;
int elso=n+1;// top. sorrend szerinti első gyerektelen
// triviálsian ettől balra lehetnek csak a gyerektelenek közös ősei
// Mindenkinek megkeresem az őseit, topologikus sorrendben veszem a pontokat, így egy pontra ez csak O(n) időbe kerül
// igazából máshogy is O(n) idő lenne, de pont a top. sorrend miatt a szülők ősei ismertek, így lehet "vagyolni", azaz gyorsítani
// az egész algoritmust!
for(d=0;d<melyseg;d++){
size=nev[d].size();
for(i=0;i<size;i++){
p=nev[d][i];
inv[p]=pos;
if(van_gyerek[p]==0&&elso==n+1)elso=pos;
if(!van_szulo[p]){meret[pos]=0;}
else{
szulo1=szulo[p][0];pos1=inv[szulo1];si1=meret[pos1];
szulo2=szulo[p][1];pos2=inv[szulo2];si2=meret[pos2];
if(si1>si2){swap(pos1,pos2);swap(si1,si2);}
si0=max(si1,si2);
if(pos1<elso)si0=max(si0,pos1);
if(pos2<elso)si0=max(si0,pos2);
meret[pos]=si0;
si=(si0+32)/32;
os[pos]=(unsigned int*)malloc(si*sizeof(unsigned int));
si1=(si1+31+(si1>0))/32;si2=(si2+31+(si2>0))/32;
for(j=0;j<si1;j++)os[pos][j]=os[pos1][j]|os[pos2][j];
for(j=si1;j<si2;j++)os[pos][j]=os[pos2][j];
for(j=si2;j<si;j++)os[pos][j]=0;
if(pos1<elso)os[pos][pos1>>5]|=bit[pos1&31];
if(pos2<elso)os[pos][pos2>>5]|=bit[pos2&31];
}
pos++;
}
}
si=(elso+31)/32;
for(j=0;j<si;j++)ans[j]=0xffffffff;
for(d=0;d<melyseg;d++){
size=nev[d].size();
for(i=0;i<size;i++){
p=nev[d][i];
if(!van_gyerek[p])
{pos=inv[p];
si2=meret[pos];
si2=(si2+31+(si2>0))/32;
for(j=0;j<si2;j++)ans[j]&=os[pos][j];}// nincs gyereke p-nek
}
}
cnt=0;
for(i=1;i<=n;i++){
pos=inv[i];
if(pos<elso&&(ans[pos>>5]&bit[pos&31]))sol[cnt++]=i;// az "i" közös ős lesz
}
printf("%d\n",cnt);
if(cnt){
for(i=0;i<cnt;i++){printf("%d",sol[i]);if(i<cnt-1)printf(" ");}
printf("\n");
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8Y3N0ZGxpY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