//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 ;
int n , m , need[MX];
int C[3];
struct abc
{
int x , y , c ;
}inp[MX];
vector <int> g[MX] ;
int col[MX];
/*
int DP(int node,int isgurd,int par)
{
if(vis[node][isgurd] ) return dp[node][isgurd];
vis[node][isgurd] = 1 ;
col[node] = 1 ;
int i , sz = g[node].size();
int sum = 0 ;
for ( i = 0 ; i < sz ; i++ )
{
int v = g[node][i];
if( v != par )
{
if(!isgurd) sum += DP(v,1,node);
else sum += min(DP(v,0,node),DP(v,1,node));
}
}
return dp[node][isgurd] = sum + isgurd ;
} */
bool dfs(int node,int c)
{
queue < int > q ;
q.push(node);
col[node] = c ;
while(!q.empty())
{
int frnt = q.front();
C[col[frnt]]++;
q.pop();
int sz = g[frnt].size();
int i , v ;
for ( i = 0 ; i < sz ; i++ )
{
v = g[frnt][i];
if( col[v] == -1 )
{
col[v] = !col[frnt];
q.push(v);
}
else if ( col[v] == col[frnt] ) return 0 ;
}
}
return 1 ;
}
void ini()
{
int i ;
for ( int i = 0 ; i <= n+1 ; i++ ) { g[i].clear();
need[i] = -1 ; // no update yet
col[i] = -1 ;
}
}
int main()
{
// ios_base::sync_with_stdio(0); cin.tie(0);
while(scanf("%d %d",&n,&m)==2)
{
map < int , int > Mark;
int idx = 0 ;
ini();
int x , y , c ;
bool ok = 1;
set <int> ans ;
rep(i,m)
{
read(inp[i].x) ;
read(inp[i].y);
read(inp[i].c);
if( inp[i].c == 2 && (need[inp[i].x] == 0 || need[inp[i].y] == 0 ) )
{
//printf("edge :: %d need[inp[i].x] :: %d need[y]::%d\n2", i,need[inp[i].x],need[inp[i].y]);
ok = 0 ;
}
else if( inp[i].c == 2 )
{
need[inp[i].x] = 1 ;
need[inp[i].y] = 1 ;
ans.insert(inp[i].x);
ans.insert(inp[i].y);
}
if( inp[i].c == 0 && ( need[inp[i].x] == 1 || need[inp[i].y] == 1) )
{
ok = 0 ;
}
else if( inp[i].c == 0 )
{
need[inp[i].x] = 0 ;
need[inp[i].x] = 0 ;
}
}
if(!ok)
{
puts("impossible");
continue;
}
rep(i,m)
{
if( inp[i].c == 2 && (need[inp[i].x] == 0 || need[inp[i].y] == 0 ) )
{
//printf("edge :: %d need[inp[i].x] :: %d need[y]::%d\n2", i,need[inp[i].x],need[inp[i].y]);
ok = 0 ;
}
if( inp[i].c == 0 && ( need[inp[i].x] == 1 || need[inp[i].y] == 1) )
{
ok = 0 ;
}
if(!ok ) break;
if(inp[i].c == 2 || inp[i].c == 0 ) continue;
if( need[inp[i].x] == 0 && need[inp[i].y] == 0 )
{ok = 0 ; // printf("edge :: %d",i);
// printf("inp[i].x :: %d need :: %d inp[i].y :: %d need :: %d\n",inp[i].x,need[inp[i].x],inp[i].y,need[inp[i].y]);
}
else if( need[inp[i].x] == 1 && need[inp[i].y] == 1 )
{ ok = 0 ;
// printf("edge:: %d\n",i);
// printf("inp[i].x :: %d need :: %d inp[i].y :: %d need :: %d\n",inp[i].x,need[inp[i].x],inp[i].y,need[inp[i].y]);
// ans.insert(inp[i].x); // 2i khanei bosathe hobe tai
// ans.insert(inp[i].y);
}
else if( (need[inp[i].x] == 1 && need[inp[i].y] == -1) || (need[inp[i].x] ==-1 && need[inp[i].y] == 1 ) )
{
if(need[inp[i].x] == 1 )
{
ans.insert(inp[i].x);
}
else ans.insert(inp[i].y);
}
else if( (need[inp[i].x] == 0 && need[inp[i].y] == -1) || (need[inp[i].x] ==-1 && need[inp[i].y] == 0 ) )
{
if(need[inp[i].x] == -1 )
{
ans.insert(inp[i].x);
}
else ans.insert(inp[i].y);
}
else {
if(Mark[inp[i].x] == 0 ) Mark[inp[i].x] = ++idx;
if(Mark[inp[i].y] == 0 ) Mark[inp[i].y] = ++idx;
int _x = Mark[inp[i].x] ;
int _y = Mark[inp[i].y];
g[_x].pb(_y);
g[_y].pb(_x);
}
if( !ok ) break;
}
if(!ok)
{
puts("impossible");
continue;
}
rep(i,m)
{
if( inp[i].c == 2 && (need[inp[i].x] == 0 || need[inp[i].y] == 0 ) )
{
//printf("edge :: %d need[inp[i].x] :: %d need[y]::%d\n2", i,need[inp[i].x],need[inp[i].y]);
ok = 0 ;
}
if( inp[i].c == 0 && ( need[inp[i].x] == 1 || need[inp[i].y] == 1) )
{
ok = 0 ;
}
if(!ok ) break;
if(inp[i].c == 2 || inp[i].c == 0 ) continue;
if( need[inp[i].x] == 0 && need[inp[i].y] == 0 )
{ok = 0 ; // printf("edge :: %d",i);
// printf("inp[i].x :: %d need :: %d inp[i].y :: %d need :: %d\n",inp[i].x,need[inp[i].x],inp[i].y,need[inp[i].y]);
}
else if( need[inp[i].x] == 1 && need[inp[i].y] == 1 )
{ ok = 0 ;
// printf("edge:: %d\n",i);
// printf("inp[i].x :: %d need :: %d inp[i].y :: %d need :: %d\n",inp[i].x,need[inp[i].x],inp[i].y,need[inp[i].y]);
// ans.insert(inp[i].x); // 2i khanei bosathe hobe tai
// ans.insert(inp[i].y);
}
if(!ok) break;
}
if(!ok)
{
puts("impossible");
continue;
}
int add = 0 ;
int i ;
// printf("hre");
for ( i = 1 ; i <= idx && ok ; i++ )
{
if(col[i]==-1)
{
C[1] = C[0] = 0 ;
ok = dfs(i,1);
add += min(C[1],C[0]);
}
}
if(!ok)
{
puts("impossible");
continue;
}
cout << ans.size() + add << endl ;
}
return 0;
}
Ly9CSVNNSUxMQUhJUlJBSE1BTklSUkFISU0KLyoKIG1hbnVzIHRhciBzaG9wbmVyIHNvbWFuIGJvcm8KIEF1dGhvciA6OiBTaGFraWwgQWhtZWQKLi4uLi4uLi4uLi4uLkFVU1RfQ1NFMjcuLi4uLi4uLi4KIHByb2IgICA6OgogVHlwZSAgIDo6CiB2ZXJkaWN0OjoKICovCgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBwYiBwdXNoX2JhY2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GR5W109ezAsMSwxLDEsMCwtMSwtMSwtMX07Ly84IGRpcmVjdGlvbgovL2ludCBkeFtdPXsyLDEsLTEsLTIsLTIsLTEsMSwyfTtpbnQgZHlbXT17MSwyLDIsMSwtMSwtMiwtMiwtMX07Ly9LbmlnaHQgRGlyZWN0aW9uCi8vaW50IGR4W109ezIsMSwtMSwtMiwtMSwxfTtpbnQgZHlbXT17MCwxLDEsMCwtMSwtMX07IC8vSGV4YWdvbmFsIERpcmVjdGlvbgoKLyogKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiogIE15IGNvZGUgc3RhcnQgaGVyZSAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiogKi8KY29uc3QgaW50IE1YID0gMjAwMDAwMCArIDUgOwppbnQgbiAsIG0gLCBuZWVkW01YXTsKaW50IENbM107CnN0cnVjdCBhYmMKewogICAgaW50IHggLCB5ICwgYyA7Cn1pbnBbTVhdOwp2ZWN0b3IgPGludD4gZ1tNWF0gOwppbnQgIGNvbFtNWF07Ci8qCmludCBEUChpbnQgbm9kZSxpbnQgaXNndXJkLGludCBwYXIpCnsKICAgIGlmKHZpc1tub2RlXVtpc2d1cmRdICkgcmV0dXJuIGRwW25vZGVdW2lzZ3VyZF07CiAgICB2aXNbbm9kZV1baXNndXJkXSA9IDEgOwogICAgY29sW25vZGVdID0gMSA7CiAgICBpbnQgaSAsIHN6ID0gZ1tub2RlXS5zaXplKCk7CiAgICBpbnQgc3VtID0gMCA7CiAgICBmb3IgKCBpID0gMCA7IGkgPCBzeiA7IGkrKyApCiAgICB7CiAgICAgICAgaW50IHYgPSBnW25vZGVdW2ldOwogICAgICAgIGlmKCB2ICE9IHBhciApCiAgICAgICAgewogICAgICAgICAgICBpZighaXNndXJkKSBzdW0gKz0gRFAodiwxLG5vZGUpOwogICAgICAgICAgICBlbHNlIHN1bSArPSBtaW4oRFAodiwwLG5vZGUpLERQKHYsMSxub2RlKSk7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGRwW25vZGVdW2lzZ3VyZF0gPSBzdW0gKyBpc2d1cmQgOwp9ICovCmJvb2wgZGZzKGludCBub2RlLGludCBjKQp7CiAgICBxdWV1ZSA8IGludCA+IHEgOwogICAgcS5wdXNoKG5vZGUpOwogICAgY29sW25vZGVdID0gYyA7CiAgICB3aGlsZSghcS5lbXB0eSgpKQogICAgewogICAgICAgIGludCBmcm50ID0gcS5mcm9udCgpOwogICAgICAgIENbY29sW2ZybnRdXSsrOwogICAgICAgIHEucG9wKCk7CiAgICAgICAgaW50IHN6ID0gZ1tmcm50XS5zaXplKCk7CiAgICAgICAgaW50IGkgLCB2IDsKICAgICAgICBmb3IgKCBpID0gMCA7IGkgPCBzeiA7IGkrKyApCiAgICAgICAgewogICAgICAgICAgICB2ID0gZ1tmcm50XVtpXTsKICAgICAgICAgICAgaWYoIGNvbFt2XSA9PSAtMSApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvbFt2XSA9ICFjb2xbZnJudF07CiAgICAgICAgICAgICAgICBxLnB1c2godik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiAoIGNvbFt2XSA9PSBjb2xbZnJudF0gKSByZXR1cm4gMCA7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDEgOwoKfQp2b2lkIGluaSgpCnsKICAgIGludCBpIDsKICAgIGZvciAoIGludCBpID0gMCA7IGkgPD0gbisxIDsgaSsrICkgeyBnW2ldLmNsZWFyKCk7CiAgICBuZWVkW2ldID0gLTEgOyAvLyBubyB1cGRhdGUgeWV0CgogICAgY29sW2ldICA9IC0xIDsKICAgIH0KfQppbnQgbWFpbigpCnsKICAgLy8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsKCiAgICB3aGlsZShzY2FuZigiJWQgJWQiLCZuLCZtKT09MikKICAgIHsKICAgICAgICBtYXAgPCBpbnQgLCBpbnQgPiBNYXJrOwogICAgICAgIGludCBpZHggPSAwIDsKICAgICAgaW5pKCk7CiAgICAgIGludCB4ICwgeSAsIGMgOwogICAgICBib29sIG9rID0gMTsKICAgICAgIHNldCA8aW50PiBhbnMgOwogICAgICByZXAoaSxtKQogICAgICB7CiAgICAgICAgICByZWFkKGlucFtpXS54KSA7CiAgICAgICAgICByZWFkKGlucFtpXS55KTsKICAgICAgICAgIHJlYWQoaW5wW2ldLmMpOwogICAgICAgICAgaWYoIGlucFtpXS5jID09IDIgJiYgKG5lZWRbaW5wW2ldLnhdID09IDAgfHwgbmVlZFtpbnBbaV0ueV0gPT0gMCApICkKICAgICAgICAgIHsKICAgICAgICAgICAgICAvL3ByaW50ZigiZWRnZSA6OiAlZCBuZWVkW2lucFtpXS54XSA6OiAlZCBuZWVkW3ldOjolZFxuMiIsIGksbmVlZFtpbnBbaV0ueF0sbmVlZFtpbnBbaV0ueV0pOwogICAgICAgICAgICAgIG9rID0gMCA7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmKCBpbnBbaV0uYyA9PSAyICkKICAgICAgICAgIHsKICAgICAgICAgICAgICBuZWVkW2lucFtpXS54XSA9IDEgOwogICAgICAgICAgICAgIG5lZWRbaW5wW2ldLnldID0gMSA7CiAgICAgICAgICAgICAgYW5zLmluc2VydChpbnBbaV0ueCk7CiAgICAgICAgICAgICAgYW5zLmluc2VydChpbnBbaV0ueSk7CiAgICAgICAgICB9CiAgICAgICAgICBpZiggaW5wW2ldLmMgPT0gMCAmJiAoIG5lZWRbaW5wW2ldLnhdID09IDEgIHx8IG5lZWRbaW5wW2ldLnldID09IDEpICkKICAgICAgICAgIHsKCiAgICAgICAgICAgICAgb2sgPSAwIDsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgaWYoIGlucFtpXS5jID09IDAgKQogICAgICAgICAgewogICAgICAgICAgICAgIG5lZWRbaW5wW2ldLnhdID0gMCA7CiAgICAgICAgICAgICAgbmVlZFtpbnBbaV0ueF0gPSAwIDsKICAgICAgICAgIH0KICAgICAgfQogICAgICBpZighb2spCiAgICAgIHsKICAgICAgICAgIHB1dHMoImltcG9zc2libGUiKTsKICAgICAgICAgIGNvbnRpbnVlOwogICAgICB9CgogICAgICByZXAoaSxtKQogICAgICB7CiAgICAgICAgICAgICBpZiggaW5wW2ldLmMgPT0gMiAmJiAobmVlZFtpbnBbaV0ueF0gPT0gMCB8fCBuZWVkW2lucFtpXS55XSA9PSAwICkgKQogICAgICAgICAgewogICAgICAgICAgICAgIC8vcHJpbnRmKCJlZGdlIDo6ICVkIG5lZWRbaW5wW2ldLnhdIDo6ICVkIG5lZWRbeV06OiVkXG4yIiwgaSxuZWVkW2lucFtpXS54XSxuZWVkW2lucFtpXS55XSk7CiAgICAgICAgICAgICAgb2sgPSAwIDsKICAgICAgICAgIH0KICAgICAgICAgICAgaWYoIGlucFtpXS5jID09IDAgJiYgKCBuZWVkW2lucFtpXS54XSA9PSAxICB8fCBuZWVkW2lucFtpXS55XSA9PSAxKSApCiAgICAgICAgICB7CgogICAgICAgICAgICAgIG9rID0gMCA7CiAgICAgICAgICB9CiAgICAgICAgICBpZighb2sgKSBicmVhazsKICAgICAgICAgIGlmKGlucFtpXS5jID09IDIgfHwgaW5wW2ldLmMgPT0gMCApIGNvbnRpbnVlOwogICAgICAgICAgaWYoIG5lZWRbaW5wW2ldLnhdID09IDAgJiYgbmVlZFtpbnBbaV0ueV0gPT0gMCApCiAgICAgICAgICB7b2sgPSAwIDsgLy8gcHJpbnRmKCJlZGdlIDo6ICVkIixpKTsKICAgICAgICAgLy8gcHJpbnRmKCJpbnBbaV0ueCA6OiAlZCBuZWVkIDo6ICVkIGlucFtpXS55IDo6ICVkIG5lZWQgOjogJWRcbiIsaW5wW2ldLngsbmVlZFtpbnBbaV0ueF0saW5wW2ldLnksbmVlZFtpbnBbaV0ueV0pOwogICAgICAgICAgfQogICAgICAgICAgZWxzZSBpZiggbmVlZFtpbnBbaV0ueF0gPT0gMSAmJiBuZWVkW2lucFtpXS55XSA9PSAxICkKICAgICAgICAgIHsgb2sgPSAwIDsKICAgICAgIC8vICAgcHJpbnRmKCJlZGdlOjogJWRcbiIsaSk7CiAgICAgICAgLy8gICBwcmludGYoImlucFtpXS54IDo6ICVkIG5lZWQgOjogJWQgaW5wW2ldLnkgOjogJWQgbmVlZCA6OiAlZFxuIixpbnBbaV0ueCxuZWVkW2lucFtpXS54XSxpbnBbaV0ueSxuZWVkW2lucFtpXS55XSk7CiAgICAgICAgICAgLy8gYW5zLmluc2VydChpbnBbaV0ueCk7IC8vIDJpIGtoYW5laSBib3NhdGhlIGhvYmUgdGFpCiAgICAgICAgICAgLy8gYW5zLmluc2VydChpbnBbaV0ueSk7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmKCAobmVlZFtpbnBbaV0ueF0gPT0gMSAmJiBuZWVkW2lucFtpXS55XSA9PSAtMSkgfHwgKG5lZWRbaW5wW2ldLnhdID09LTEgJiYgbmVlZFtpbnBbaV0ueV0gPT0gMSApICkKICAgICAgICAgIHsKICAgICAgICAgICAgICBpZihuZWVkW2lucFtpXS54XSA9PSAxICkKICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgIGFucy5pbnNlcnQoaW5wW2ldLngpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBlbHNlIGFucy5pbnNlcnQoaW5wW2ldLnkpOwogICAgICAgICAgfQogICAgICAgICAgICAgZWxzZSBpZiggKG5lZWRbaW5wW2ldLnhdID09IDAgJiYgbmVlZFtpbnBbaV0ueV0gPT0gLTEpIHx8IChuZWVkW2lucFtpXS54XSA9PS0xICYmIG5lZWRbaW5wW2ldLnldID09IDAgKSApCiAgICAgICAgICB7CiAgICAgICAgICAgICAgaWYobmVlZFtpbnBbaV0ueF0gPT0gLTEgKQogICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgYW5zLmluc2VydChpbnBbaV0ueCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGVsc2UgYW5zLmluc2VydChpbnBbaV0ueSk7CiAgICAgICAgICB9CiAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgaWYoTWFya1tpbnBbaV0ueF0gPT0gMCApIE1hcmtbaW5wW2ldLnhdID0gKytpZHg7CiAgICAgICAgICAgIGlmKE1hcmtbaW5wW2ldLnldID09IDAgKSBNYXJrW2lucFtpXS55XSA9ICsraWR4OwogICAgICAgICAgICBpbnQgX3ggPSBNYXJrW2lucFtpXS54XSA7CiAgICAgICAgICAgIGludCBfeSA9IE1hcmtbaW5wW2ldLnldOwoKICAgICAgICAgIGdbX3hdLnBiKF95KTsKICAgICAgICAgIGdbX3ldLnBiKF94KTsKICAgICAgICAgIH0KICAgICAgICAgIGlmKCAhb2sgKSBicmVhazsKCgogICAgICB9CiAgICAgICAgaWYoIW9rKQogICAgICB7CiAgICAgICAgICBwdXRzKCJpbXBvc3NpYmxlIik7CiAgICAgICAgICBjb250aW51ZTsKICAgICAgfQogICAgICByZXAoaSxtKQogICAgICB7CiAgICAgICAgICAgICAgaWYoIGlucFtpXS5jID09IDIgJiYgKG5lZWRbaW5wW2ldLnhdID09IDAgfHwgbmVlZFtpbnBbaV0ueV0gPT0gMCApICkKICAgICAgICAgIHsKICAgICAgICAgICAgICAvL3ByaW50ZigiZWRnZSA6OiAlZCBuZWVkW2lucFtpXS54XSA6OiAlZCBuZWVkW3ldOjolZFxuMiIsIGksbmVlZFtpbnBbaV0ueF0sbmVlZFtpbnBbaV0ueV0pOwogICAgICAgICAgICAgIG9rID0gMCA7CiAgICAgICAgICB9CiAgICAgICAgICAgIGlmKCBpbnBbaV0uYyA9PSAwICYmICggbmVlZFtpbnBbaV0ueF0gPT0gMSAgfHwgbmVlZFtpbnBbaV0ueV0gPT0gMSkgKQogICAgICAgICAgewoKICAgICAgICAgICAgICBvayA9IDAgOwogICAgICAgICAgfQogICAgICAgICAgaWYoIW9rICkgYnJlYWs7CiAgICAgICAgICBpZihpbnBbaV0uYyA9PSAyIHx8IGlucFtpXS5jID09IDAgKSBjb250aW51ZTsKICAgICAgICAgIGlmKCBuZWVkW2lucFtpXS54XSA9PSAwICYmIG5lZWRbaW5wW2ldLnldID09IDAgKQogICAgICAgICAge29rID0gMCA7IC8vIHByaW50ZigiZWRnZSA6OiAlZCIsaSk7CiAgICAgICAgIC8vIHByaW50ZigiaW5wW2ldLnggOjogJWQgbmVlZCA6OiAlZCBpbnBbaV0ueSA6OiAlZCBuZWVkIDo6ICVkXG4iLGlucFtpXS54LG5lZWRbaW5wW2ldLnhdLGlucFtpXS55LG5lZWRbaW5wW2ldLnldKTsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgaWYoIG5lZWRbaW5wW2ldLnhdID09IDEgJiYgbmVlZFtpbnBbaV0ueV0gPT0gMSApCiAgICAgICAgICB7IG9rID0gMCA7CiAgICAgICAvLyAgIHByaW50ZigiZWRnZTo6ICVkXG4iLGkpOwogICAgICAgIC8vICAgcHJpbnRmKCJpbnBbaV0ueCA6OiAlZCBuZWVkIDo6ICVkIGlucFtpXS55IDo6ICVkIG5lZWQgOjogJWRcbiIsaW5wW2ldLngsbmVlZFtpbnBbaV0ueF0saW5wW2ldLnksbmVlZFtpbnBbaV0ueV0pOwogICAgICAgICAgIC8vIGFucy5pbnNlcnQoaW5wW2ldLngpOyAvLyAyaSBraGFuZWkgYm9zYXRoZSBob2JlIHRhaQogICAgICAgICAgIC8vIGFucy5pbnNlcnQoaW5wW2ldLnkpOwogICAgICAgICAgfQogICAgICAgICAgaWYoIW9rKSBicmVhazsKICAgICAgfQogICAgICAgIGlmKCFvaykKICAgICAgewogICAgICAgICAgcHV0cygiaW1wb3NzaWJsZSIpOwogICAgICAgICAgY29udGludWU7CiAgICAgIH0KICAgICAgaW50IGFkZCA9IDAgOwogICAgICBpbnQgaSA7CiAgICAvLyAgcHJpbnRmKCJocmUiKTsKICAgICAgZm9yICggaSA9ICAxIDsgaSA8PSBpZHggJiYgb2sgOyBpKysgKQogICAgICB7CiAgICAgICAgICBpZihjb2xbaV09PS0xKQogICAgICAgICAgewogICAgICAgICAgICAgIENbMV0gPSBDWzBdID0gMCA7CiAgICAgICAgICAgICAgb2sgPSBkZnMoaSwxKTsKICAgICAgICAgICAgICBhZGQgKz0gbWluKENbMV0sQ1swXSk7CiAgICAgICAgICB9CgogICAgICB9CiAgICAgICAgaWYoIW9rKQogICAgICB7CiAgICAgICAgICBwdXRzKCJpbXBvc3NpYmxlIik7CiAgICAgICAgICBjb250aW51ZTsKICAgICAgfQogICAgICBjb3V0IDw8IGFucy5zaXplKCkgKyBhZGQgPDwgZW5kbCA7CgoKCgogICAgfQoKCgogICAgcmV0dXJuIDA7Cn0=