//BISMILLAHIRRAHMANIRRAHIM
/*
manus tar shopner soman boro
Author :: Shakil Ahmed
.............AUST_CSE27.........
prob ::
Type ::
verdict::
*/
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pi acos(-1.0)
#define ff first
#define ss second
#define re return
#define QI queue<int>
#define SI stack<int>
#define SZ(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define memo(a,b) memset((a),(b),sizeof(a))
#define G() getchar()
#define MAX3(a,b,c) max(a,max(b,c))
double const EPS=3e-8;
using namespace std;
#define FI freopen ("input.txt", "r", stdin)
#define FO freopen ("output.txt", "w", stdout)
typedef long long Long;
typedef long long int64;
typedef unsigned long long ull;
typedef vector<int> vi ;
typedef set<int> si;
typedef vector<Long>vl;
typedef pair<int,int>pii;
typedef pair<string,int>psi;
typedef pair<Long,Long>pll;
typedef pair<double,double>pdd;
typedef vector<pii> vpii;
// For loop
#define forab(i, a, b) for (__typeof (b) i = (a) ; i <= b ; ++i)
#define rep(i, n) forab (i, 0, (n) - 1)
#define For(i, n) forab (i, 1, n)
#define rofba(i, a, b) for (__typeof (b)i = (b) ; i >= a ; --i)
#define per(i, n) rofba (i, 0, (n) - 1)
#define rof(i, n) rofba (i, 1, n)
#define forstl(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
//Fast Reader
template<class T>inline bool read(T &x){int c=getchar();int sgn=1;while(~c&&c<'0'||c>'9'){if(c=='-')sgn=-1;c=getchar();}for(x=0;~c&&'0'<=c&&c<='9';c=getchar())x=x*10+c-'0'; x*=sgn; return ~c;}
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction
/* ************************************** My code start here ****************************************** */
const int MX = 2000000 + 5 ;
vector < pii > g[MX];
vector <int> Adj[MX];
int n , m , color[MX] , vis , col[2] ;
void ini()
{
int i ;
for ( i = 0 ; i <= n ; i++ )
{
g[i].clear();
Adj[i].clear();
color[i] = -1; // haven't updated yet
}
}
bool dfs(int x , int c,int par)
{
color[x] = c ;
col[c]++;
int sz = Adj[x].size();
for ( int i = 0 ; i < sz ; i++ )
{
int u = Adj[x][i];
if( par == u ) continue ;
if( color[u] == -1 && !dfs(u,!c,x)) return 0;
else
{
if( color[u] == c ) return 0;
}
}
return 1 ;
}
bool ok(int x , int col)
{
// here col is fixed color
}
int main()
{
// ios_base::sync_with_stdio(0); cin.tie(0);
while(scanf("%d %d",&n,&m)==2)
{
map < int , int > VIS ;
int u , v , c , i ;
ini();
bool ok = 1 ;
for ( i = 0 ; i < m ; i++ )
{
cin >> u >> v >> c ;
if( c == 0 && ( color[u] == 1 || color[v] == 1 ) ) // As its 0 , its adjacent should be also zero
{
ok = 0 ;
}
else if( c == 0 )
{
color[u] = 0 ;
color[v] = 0 ;
}
else if( c == 2 && ( color[u] == 0 || color[v] == 0 ) ) // As its 1 , its Adjacent must be 1
{
ok = 0 ;
}
else if( c == 2 )
{
color[u] = 1 ;
color[v] = 1 ;
}
g[u].pb(mp(v,c));
g[v].pb(mp(u,c));
}
// 1st part is complete now i calculate all number 1 color vertex and
// try to check bi-coloring between those who has -1 coloring
for (int i = 1 ; i <= n && ok ; i++ )
{
if ( color[i] == 1 )
{
// As its 1 all vextexes connected with it 1 , must be 1
// or connected with it by -1 must be 0
int sz = g[i].size();
int x ;
for( x = 0 ; x < sz ; x++ )
{
int c = g[i][x].second ;
int v = g[i][x].first ;
if ( c == 2 && color[v] == 0 )
{
ok = 0 ; // conflict as it both the node should be 1
}
else if( c == 1 && color[v] == -1 )
{
color[v] = 0 ; // it must be 0
}
else
{
// c is 0 it can't be possible
ok = 0 ;
}
}
}
else if( color[i] == 0 )
{
// As its 0 all vextexes connected with it 1 , must be 0
// or connected with it by -1 must be 1 if c is 1
int sz = g[i].size();
int x ;
for( x = 0 ; x < sz ; x++ )
{
int c = g[i][x].second ;
int v = g[i][x].first ;
if ( c == 0 && color[v] == 1 )
{
ok = 0 ; // conflict as it both the node should be 0
}
else if( c == 1 && color[v] == -1 )
{
color[v] = 1 ; // it must be 0
}
else
{
// c is 2 it can't be possible
ok = 0 ;
}
}
}
}
if( !ok ) // its not possible satisfied all equation
{
puts("impossible");
continue;
}
int Ans = 0 , sum = 0 ;
// Now construct a bi-coloring graph where both end vectex color is write now -1 ;
int idx = 0 ;
for ( int i = 1 ; i <= n ; i++ )
{
if( color[i] == 1 ) // here must be a lounch
Ans++;
else if( color[i] == -1)
{
if( VIS.find(i) == VIS.end() )
{
VIS[i] = ++idx ;
}
int sz = g[i].size();
int x , y , z ;
x = VIS[i];
for ( z = 0 ; z < sz ; z++ )
{
int v = g[i][z].first ;
y = v ;
int c = g[i][z].second;
if( VIS.find(v) == VIS.end() )
{
VIS[v] = ++idx ;
}
y = VIS[v];
if( c == 1 && color[v] == -1 ) // there must be a edge in bi_coloring graph
{
Adj[x].pb(y);
}
}
}
}
for (int i = 1 ; i <= idx && ok ; i++ )
{
if( color[i] == -1 )
{
col[0] = col[1] = 0 ;
ok = dfs(i,1,-1);
Ans += min(col[0],col[1]);
}
}
if( !ok ) // its not possible satisfied all equation
{
puts("impossible");
continue;
}
else
{
cout << Ans << endl ;
}
}
return 0;
}
Ly9CSVNNSUxMQUhJUlJBSE1BTklSUkFISU0KLyoKIG1hbnVzIHRhciBzaG9wbmVyIHNvbWFuIGJvcm8KIEF1dGhvciA6OiBTaGFraWwgQWhtZWQKLi4uLi4uLi4uLi4uLkFVU1RfQ1NFMjcuLi4uLi4uLi4KIHByb2IgICA6OgogVHlwZS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gZHlbXT17MCwxLDAsLTF9OyAvLzQgRGlyZWN0aW9uCi8vaW50IGR4W109ezEsMSwwLC0xLC0xLC0xLDAsMX07aW50IGR5W109ezAsMSwxLDEsMCwtMSwtMSwtMX07Ly84IGRpcmVjdGlvbgovL2ludCBkeFtdPXsyLDEsLTEsLTIsLTIsLTEsMSwyfTtpbnQgZHlbXT17MSwyLDIsMSwtMSwtMiwtMiwtMX07Ly9LbmlnaHQgRGlyZWN0aW9uCi8vaW50IGR4W109ezIsMSwtMSwtMiwtMSwxfTtpbnQgZHlbXT17MCwxLDEsMCwtMSwtMX07IC8vSGV4YWdvbmFsIERpcmVjdGlvbgoKLyogKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiogIE15IGNvZGUgc3RhcnQgaGVyZSAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiogKi8KY29uc3QgaW50IE1YID0gMjAwMDAwMCArIDUgOwp2ZWN0b3IgPCBwaWkgPiBnW01YXTsKdmVjdG9yIDxpbnQ+IEFkaltNWF07CmludCBuICwgbSAsIGNvbG9yW01YXSAsIHZpcyAsIGNvbFsyXSA7Cgp2b2lkIGluaSgpCnsKICAgIGludCBpIDsKICAgIGZvciAoIGkgPSAwIDsgaSA8PSBuIDsgaSsrICkKICAgIHsKICAgICAgICBnW2ldLmNsZWFyKCk7CiAgICAgICAgQWRqW2ldLmNsZWFyKCk7CiAgICAgICAgY29sb3JbaV0gPSAtMTsgLy8gaGF2ZW4ndCB1cGRhdGVkIHlldAogICAgfQp9CmJvb2wgZGZzKGludCB4ICwgaW50IGMsaW50IHBhcikKewogICAgY29sb3JbeF0gPSBjIDsKICAgIGNvbFtjXSsrOwogICAgaW50IHN6ID0gQWRqW3hdLnNpemUoKTsKICAgIGZvciAoIGludCBpID0gMCA7IGkgPCBzeiA7IGkrKyApCiAgICB7CiAgICAgICAgaW50IHUgPSBBZGpbeF1baV07CiAgICAgICAgaWYoIHBhciA9PSB1ICkgY29udGludWUgOwogICAgICAgIGlmKCBjb2xvclt1XSA9PSAtMSAmJiAhZGZzKHUsIWMseCkpIHJldHVybiAwOwogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGlmKCBjb2xvclt1XSA9PSBjICkgcmV0dXJuIDA7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDEgOwp9CmJvb2wgb2soaW50IHggLCBpbnQgY29sKQp7CiAgICAvLyBoZXJlIGNvbCBpcyBmaXhlZCBjb2xvcgp9CmludCBtYWluKCkKewogICAvLyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOwoKICAgIHdoaWxlKHNjYW5mKCIlZCAlZCIsJm4sJm0pPT0yKQogICAgewogICAgICAgIG1hcCA8IGludCAsIGludCA+IFZJUyA7CiAgICAgICAgaW50IHUgLCB2ICwgYyAsIGkgOwogICAgICAgIGluaSgpOwogICAgICAgIGJvb2wgb2sgPSAxIDsKICAgICAgICBmb3IgKCBpID0gMCA7IGkgPCBtIDsgaSsrICkKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiB1ID4+IHYgPj4gYyA7CgogICAgICAgICAgICBpZiggYyA9PSAwICYmICggY29sb3JbdV0gPT0gMSB8fCBjb2xvclt2XSA9PSAxICkgKSAvLyBBcyBpdHMgMCAsIGl0cyBhZGphY2VudCBzaG91bGQgYmUgYWxzbyB6ZXJvCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG9rID0gMCA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiggYyA9PSAwICApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvbG9yW3VdID0gMCA7CiAgICAgICAgICAgICAgICBjb2xvclt2XSA9IDAgOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYoIGMgPT0gMiAmJiAoIGNvbG9yW3VdID09IDAgfHwgY29sb3Jbdl0gPT0gMCApICkgLy8gQXMgaXRzIDEgLCBpdHMgQWRqYWNlbnQgbXVzdCBiZSAxCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG9rID0gMCA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiggYyA9PSAyICkKICAgICAgICAgICAgewoKICAgICAgICAgICAgICAgIGNvbG9yW3VdID0gMSA7CiAgICAgICAgICAgICAgICBjb2xvclt2XSA9IDEgOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGdbdV0ucGIobXAodixjKSk7CiAgICAgICAgICAgIGdbdl0ucGIobXAodSxjKSk7CiAgICAgICAgfQoKICAgICAgICAvLyAxc3QgcGFydCBpcyBjb21wbGV0ZSBub3cgaSBjYWxjdWxhdGUgYWxsIG51bWJlciAxIGNvbG9yIHZlcnRleCBhbmQKICAgICAgICAvLyB0cnkgdG8gY2hlY2sgYmktY29sb3JpbmcgYmV0d2VlbiB0aG9zZSB3aG8gaGFzIC0xIGNvbG9yaW5nCgogICAgICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBuICYmIG9rIDsgaSsrICkKICAgICAgICB7CgogICAgICAgICAgICBpZiAoIGNvbG9yW2ldID09IDEgKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAvLyBBcyBpdHMgMSAgYWxsIHZleHRleGVzIGNvbm5lY3RlZCB3aXRoIGl0IDEgLCBtdXN0IGJlIDEKICAgICAgICAgICAgICAgIC8vIG9yIGNvbm5lY3RlZCB3aXRoIGl0IGJ5IC0xIG11c3QgYmUgMAogICAgICAgICAgICAgICAgaW50IHN6ID0gZ1tpXS5zaXplKCk7CiAgICAgICAgICAgICAgICBpbnQgeCA7CiAgICAgICAgICAgICAgICBmb3IoIHggPSAwIDsgeCA8IHN6IDsgeCsrICkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnQgYyA9IGdbaV1beF0uc2Vjb25kIDsKICAgICAgICAgICAgICAgICAgICBpbnQgdiA9IGdbaV1beF0uZmlyc3QgOwogICAgICAgICAgICAgICAgICAgIGlmICggYyA9PSAyICYmIGNvbG9yW3ZdID09IDAgKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgb2sgPSAwIDsgLy8gY29uZmxpY3QgYXMgaXQgYm90aCB0aGUgbm9kZSBzaG91bGQgYmUgMQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKCBjID09IDEgJiYgY29sb3Jbdl0gPT0gLTEgKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgY29sb3Jbdl0gPSAwIDsgLy8gaXQgbXVzdCBiZSAwCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vIGMgaXMgMCBpdCBjYW4ndCBiZSBwb3NzaWJsZQogICAgICAgICAgICAgICAgICAgICAgICBvayA9IDAgOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKCBjb2xvcltpXSA9PSAwICkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgIC8vIEFzIGl0cyAwICBhbGwgdmV4dGV4ZXMgY29ubmVjdGVkIHdpdGggaXQgMSAsIG11c3QgYmUgMAogICAgICAgICAgICAgICAgLy8gb3IgY29ubmVjdGVkIHdpdGggaXQgYnkgLTEgbXVzdCBiZSAxIGlmIGMgaXMgMQogICAgICAgICAgICAgICAgaW50IHN6ID0gZ1tpXS5zaXplKCk7CiAgICAgICAgICAgICAgICBpbnQgeCA7CiAgICAgICAgICAgICAgICBmb3IoIHggPSAwIDsgeCA8IHN6IDsgeCsrICkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnQgYyA9IGdbaV1beF0uc2Vjb25kIDsKICAgICAgICAgICAgICAgICAgICBpbnQgdiA9IGdbaV1beF0uZmlyc3QgOwogICAgICAgICAgICAgICAgICAgIGlmICggYyA9PSAwICYmIGNvbG9yW3ZdID09IDEgKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgb2sgPSAwIDsgLy8gY29uZmxpY3QgYXMgaXQgYm90aCB0aGUgbm9kZSBzaG91bGQgYmUgMAogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKCBjID09IDEgJiYgY29sb3Jbdl0gPT0gLTEgKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgY29sb3Jbdl0gPSAxIDsgLy8gaXQgbXVzdCBiZSAwCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vIGMgaXMgMiBpdCBjYW4ndCBiZSBwb3NzaWJsZQogICAgICAgICAgICAgICAgICAgICAgICBvayA9IDAgOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiggIW9rICkgLy8gaXRzIG5vdCBwb3NzaWJsZSBzYXRpc2ZpZWQgYWxsIGVxdWF0aW9uCiAgICAgICAgewogICAgICAgICAgICBwdXRzKCJpbXBvc3NpYmxlIik7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KICAgICAgICBpbnQgQW5zID0gMCAsIHN1bSA9IDAgOwogICAgICAgIC8vIE5vdyBjb25zdHJ1Y3QgYSBiaS1jb2xvcmluZyBncmFwaCB3aGVyZSBib3RoIGVuZCB2ZWN0ZXggY29sb3IgaXMgd3JpdGUgbm93IC0xIDsKICAgICAgICBpbnQgaWR4ID0gMCA7CiAgICAgICAgZm9yICggaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrICkKICAgICAgICB7CgogICAgICAgICAgICBpZiggY29sb3JbaV0gPT0gMSApIC8vIGhlcmUgbXVzdCBiZSBhIGxvdW5jaAogICAgICAgICAgICBBbnMrKzsKICAgICAgICAgICAgZWxzZSBpZiggY29sb3JbaV0gPT0gLTEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKCBWSVMuZmluZChpKSA9PSBWSVMuZW5kKCkgKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIFZJU1tpXSA9ICsraWR4IDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGludCBzeiAgPSBnW2ldLnNpemUoKTsKICAgICAgICAgICAgICAgIGludCB4ICwgeSAsIHogOwogICAgICAgICAgICAgICAgeCA9IFZJU1tpXTsKICAgICAgICAgICAgICAgIGZvciAoICB6ID0gMCA7IHogPCBzeiA7IHorKyApCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaW50IHYgPSBnW2ldW3pdLmZpcnN0IDsKICAgICAgICAgICAgICAgICAgICB5ICA9IHYgOwogICAgICAgICAgICAgICAgICAgIGludCBjID0gZ1tpXVt6XS5zZWNvbmQ7CiAgICAgICAgICAgICAgICAgICAgaWYoIFZJUy5maW5kKHYpID09IFZJUy5lbmQoKSApCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBWSVNbdl0gPSArK2lkeCA7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIHkgPSBWSVNbdl07CiAgICAgICAgICAgICAgICAgICAgaWYoIGMgPT0gMSAmJiAgY29sb3Jbdl0gPT0gLTEgKSAvLyB0aGVyZSBtdXN0IGJlIGEgZWRnZSBpbiBiaV9jb2xvcmluZyBncmFwaAogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgQWRqW3hdLnBiKHkpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gaWR4ICYmIG9rIDsgaSsrICkKICAgICAgICB7CiAgICAgICAgICAgIGlmKCBjb2xvcltpXSA9PSAtMSApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvbFswXSA9IGNvbFsxXSA9IDAgOwogICAgICAgICAgICAgICAgb2sgPSBkZnMoaSwxLC0xKTsKICAgICAgICAgICAgICAgIEFucyArPSBtaW4oY29sWzBdLGNvbFsxXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgIGlmKCAhb2sgKSAvLyBpdHMgbm90IHBvc3NpYmxlIHNhdGlzZmllZCBhbGwgZXF1YXRpb24KICAgICAgICB7CiAgICAgICAgICAgIHB1dHMoImltcG9zc2libGUiKTsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGNvdXQgPDwgQW5zIDw8IGVuZGwgOwogICAgICAgIH0KCiAgICB9CgoKCiAgICByZXR1cm4gMDsKfQo=