//https://www.luogu.com.cn/problem/P10935
//https://www.acwing.com/problem/content/description/370/
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=(N*2+N)*2;
typedef long long ll;
int n,m;
int e[M],ne[M],h[N],idx,w[M],hs[N];
int dfn[N],low[N],timestamp;
int stk[N],top;
bool in_stk[N];
int id[N],scc_cnt,sz[N];
int dist[N];
void add(int h[],int a,int b,int c){
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void tarjan(int u){
dfn[u]=low[u]=++timestamp;
stk[top++]=u,in_stk[u]=true;
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(!dfn[j]){
tarjan(j);
low[u]=min(low[u],low[j]);
}else if(in_stk[j]) low[u]=min(low[u],dfn[j]);
}
if(dfn[u]==low[u]){
scc_cnt++;
int y;
do{
y=stk[--top];
in_stk[y]=false;
id[y]=scc_cnt;
sz[scc_cnt]++;
}while(y!=u);
}
}
int main(){
memset(h,-1,sizeof h);
memset(hs,-1,sizeof hs);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) add(h,0,i,1);
for(int i=0,type,a,b;i<m;i++){
scanf("%d%d%d",&type,&a,&b);
if(type==1) add(h,a,b,0),add(h,b,a,0);
else if(type==2) add(h,a,b,1);
else if(type==3) add(h,b,a,0);
else if(type==4) add(h,b,a,1);
else add(h,a,b,0);
}
tarjan(0);
bool success=true;
for(int i=0;i<=n;i++){
for(int j=h[i];~j;j=ne[j]){
int k=e[j];
int a=id[i],b=id[k];
if(a==b){
if(w[j]>0){
success=false;
break;
}
}else add(hs,a,b,w[j]);
}
}
if(!success) puts("-1");
else{
for(int i=scc_cnt;i;i--){
for(int j=hs[i];~j;j=ne[j]){
int k=e[j];
dist[k]=max(dist[k],dist[i]+w[j]);
}
}
ll res=0;
for(int i=1;i<=scc_cnt;i++) res+=(ll)dist[i]*sz[i];
printf("%lld\n",res);
}
return 0;
}