#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	vector<int> v1 = {};
	vector<int> v2 = {};
	vector<int> v3 = {};
	ll sum = 0;
	ll N, M;
    ll a,b,t;
    ll u,v;
	cin >> N >> M;
    for (int i = 0; i < M; i++){
        cin >> a >> b >> t;
		v1.push_back(a);
		v2.push_back(b);
		v3.push_back(t);
    }
	for(int n = 0; n <= M; n++){
		if(v1[n] == v1[n+1] && v1[n] == v2[n+1] && t == 0){
			sum+=1;
		}
		if(v2[n] == v1[n+1] && v2[n] == v2[n+1] && t == 0){
			sum+=1;
		}
		else{
			sum+=0;
		}
	}
    cin >> u >> v;
	sum += accumulate(v3.begin(), v3.end(), 0);
	cout << sum;
}