1 /****************************************************
2
3 Author : Coolxxx
4 Copyright 2017 by Coolxxx. All rights reserved.
5 BLOG : http://blog.csdn.net/u010568270
6
7 ****************************************************/
8 #include<bits/stdc++.h>
9 #pragma comment(linker,"/STACK:1024000000,1024000000")
10 #define abs(a) ((a)>0?(a):(-(a)))
11 #define lowbit(a) (a&(-a))
12 #define sqr(a) ((a)*(a))
13 #define mem(a,b) memset(a,b,sizeof(a))
14 const double EPS=1e-8;
15 const int J=10;
16 const int MOD=100000007;
17 const int MAX=0x7f7f7f7f;
18 const double PI=3.14159265358979323;
19 const int N=200004;
20 using namespace std;
21 typedef long long LL;
22 double anss;
23 LL aans;
24 int cas,cass;
25 int n,m,lll,ans;
26 int q[N],last[N];
27 int mark[N][2];
28 bool u[N];
29 struct xxx
30 {
31 int next,to,d;
32 }a[N+N];
33 void add(int x,int y,int z)
34 {
35 a[++lll].to=y;
36 a[lll].d=z;
37 a[lll].next=last[x];
38 last[x]=lll;
39 }
40 bool spfa(int s)
41 {
42 int i,l=0,r=1,now,to,sumz=0,sumo=0;
43 bool z=0,o=0;
44 q[1]=s;mark[s][0]=0,mark[s][1]=1;u[s]=1;
45 while(l!=r)
46 {
47 now=q[l=(l+1)%N];
48 sumz+=mark[now][0];sumo+=mark[now][1];
49 for(i=last[now];i;i=a[i].next)
50 {
51 to=a[i].to;
52 if(mark[to][0]==-1)mark[to][0]=a[i].d-mark[now][0];
53 if(mark[to][1]==-1)mark[to][1]=a[i].d-mark[now][1];
54 if(mark[now][0]+mark[to][0]!=a[i].d || mark[to][0]<0 || mark[to][0]>1)z=1;
55 if(mark[now][1]+mark[to][1]!=a[i].d || mark[to][1]<0 || mark[to][1]>1)o=1;
56 if(!u[to])
57 {
58 u[to]=1;
59 q[r=(r+1)%N]=to;
60 }
61 }
62 if(z && o)break;
63 }
64 if(z && o)return 0;
65 if(!z && o)ans+=sumz;
66 else if(z && !o)ans+=sumo;
67 else if(!z && !o)ans+=min(sumz,sumo);
68 return 1;
69 }
70 int main()
71 {
72 #ifndef ONLINE_JUDGE
73 freopen("1.txt","r",stdin);
74 // freopen("2.txt","w",stdout);
75 #endif
76 int i,j,k;
77 int x,y,z;
78 // for(scanf("%d",&cass);cass;cass--)
79 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
80 // while(~scanf("%s",s))
81 while(~scanf("%d",&n))
82 {
83 mem(u,0);mem(mark,-1);
84 scanf("%d",&m);
85 for(i=1;i<=m;i++)
86 {
87 scanf("%d%d%d",&x,&y,&z);
88 add(x,y,z);
89 add(y,x,z);
90 }
91 ans=0;
92 for(i=1;i<=n;i++)
93 {
94 if(u[i])continue;
95 if(!spfa(i))break;
96 }
97 if(i<=n)puts("impossible");
98 else printf("%d\n",ans);
99 }
100 return 0;
101 }
102 /*
103 //
104
105 //
106 */