#include <iostream>
#include <vector>
#include <set>
using namespace std;

void dfs(vector<vector<pair<int,int>>> &graph, 
    set<int> &visited, set<int> &reachable, vector<int> &topological_order, int v) {
    visited.insert(v);
    reachable.insert(v);
    for (pair<int,int> neighbour : graph[v]) {
        if (visited.count(neighbour.first)==0) {
            dfs(graph, visited, reachable, topological_order, neighbour.first);
        }
    }
    topological_order.push_back(v);
}

bool dfs_cycle(vector<vector<pair<int,int>>> &graph, set<int> &visited, set<int> &path, int v) {
    visited.insert(v);
    path.insert(v);
    for (pair<int,int> neighbour : graph[v]) {
        if (visited.count(neighbour.first)==0) {
            if (dfs_cycle(graph, visited, path, neighbour.first)) {
                return true;
            }
        } else if (path.count(neighbour.first)==1) {
            return true;
        }
    }
    path.erase(v);
    return false;
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
    int n, m;
    cin >>n>>m;

    vector<vector<pair<int,int>>> graph(n);
    
    for (int i=0; i<m; i++) {
        int a,b,c;
        cin >>a >>b >>c;
        if (a != n && b != 1) {
            graph[a-1].push_back(make_pair(b-1, c));
        }
    }

    set<int> visited;
    set<int> path;
    set<int> reachable;
    vector<int> topological_order;

    dfs(graph, visited, reachable, topological_order, 0);
    if (reachable.count(n-1)==0) {
        //nie ma drogi do szkoły
        cout <<"BRAK"<<endl;
        return 0;
    }

    visited.clear();
    if (dfs_cycle(graph, visited, path,0)) {
        //graf ma cykl, idziemy na wagary
        cout <<"WAGARY"<<endl;
        return 0;
    }

    // graf nie ma cyklu, jest DAGiem, szukam najdłuższych ścieżek programowaniem dynamicznym
    int *LP = new int[n];   //najdłuższe ścieżki od każdego węzła do szkoły
    for (int i=0; i<n; i++) {
        LP[i] = -20000000;  // bugfix - nieosiągające szkoły mają mieć < 0
    }
    LP[n-1] = 0;

    int i=0;
    while (topological_order[i]!=n-1) i++;
    i++;
    for (; i<topological_order.size(); i++) {
        int v = topological_order[i];
        for (pair<int,int> neighbour : graph[v]) {
            LP[v] = max(LP[v], neighbour.second + LP[neighbour.first]);
        }
    }

    cout <<LP[0]<<endl;
}