Problem:
Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let m = scan.read::<usize>();
    let mut g = vec![vec![];n];
    for _ in 0..m {
        let a = scan.read::<usize>()-1;
        let b = scan.read::<usize>()-1;
        let c = scan.read::<i64>();
        g[a].push((c, b));
    }
    let mut dag = Dag::new(g);
    dag.tsort();
    if !dag.is_acyclic() {
        println!("inf");
    } else {
        println!("{}", dag.longest_path(0, n-1));
    }
}

struct Dag {
    g: Vec<Vec<(i64, usize)>>,
    vs: Vec<usize>,
    n: usize
}

#[allow(dead_code)]
impl Dag {
    fn new(g: Vec<Vec<(i64, usize)>>) -> Dag {
        let n = g.len();
        Dag {n: n, g: g.clone(), vs: vec![]}
    }

    fn tsort(&mut self) {
        let mut h = vec![0usize;self.n];
        for i in 0..self.n {
            for &(_, v) in &self.g[i] {h[v] += 1;}
        }
        let mut stack = vec![];
        for i in 0..self.n {if h[i] == 0 {stack.push(i);}}
        while let Some(i) = stack.pop() {
            self.vs.push(i);
            for &(_, j) in &self.g[i] {
                h[j] -= 1;
                if h[j] == 0 {stack.push(j);}
            }
        }
    }

    fn is_acyclic(&self) -> bool {
        self.vs.len() == self.n
    }

    fn is_unique(&self) -> bool {
        for i in 0..self.n - 1 {
            let a = self.vs[i];
            let b = self.vs[i+1];
            let mut f = false;
            for &(_, v) in &self.g[a] {
                if v == b {
                    f = true;
                    break;
                }
            }
            if !f {return false;}
        }
        true
    }

    fn longest_path(&self, s: usize, t: usize) -> i64 {
        let mut dp = vec![-1i64 << 60;self.n];
        dp[s] = 0;
        for &p in &self.vs {
            for &(c, v) in &self.g[p] {
                dp[v] = std::cmp::max(dp[v], dp[p] + c);
            }
        }
        dp[t] - dp[s]
    }
}

// region template
#[allow(dead_code)]
fn iu(i: isize) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> isize { i as isize }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion