Problem: Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.

Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.

They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.

They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.

Now, you have a figure of room layout. Your task is written below.

You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.

To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.

Please answer the lowest cost.

By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.

Constraints

* 2 ≤ n  ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.

Input

The input consists of multiple datasets. Each dataset is given in the following format.


n m
X1 Y1 C1
...
Xm Ym Cm


All numbers in each datasets are integers. The integers in each line are separated by a space.

The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.

Each of following  m  lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).

Output

For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.

Example

Input

3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0


Output

1
100
0
-1

Solution (in CPP):

Solution:
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) {return (n==0)?1:10*TEN(n-1);}
int bsr(int x) { return 31 - __builtin_clz(x); }

template <class E>
using Graph = vector<vector<E>>;

// NOT VERIFY
template<class D, D INF>
struct BidirectedCut {
    D sum = INF;
    template<class E>
    BidirectedCut(Graph<E> g) {
        int n = (int)g.size();
        int m_a = -1, m_b = -1;
        vector<D> dist_base(n, 0);
        for (int m = n; m > 1; m--) {
            int a, b;
            auto dist = dist_base;
            for (int i = 0; i < m; i++) {
                a = b; b = max_element(begin(dist), end(dist)) - begin(dist);
                if (i == m-1) sum = min(sum, dist[b]);
                dist[b] = -INF;
                for (E &e: g[b]) {
                    if (e.to == m_b) e.to = m_a;
                    if (dist[e.to] == -INF) continue;
                    dist[e.to] += e.dist;
                }
            }
            //merge a, b
            m_a = a; m_b = b;
            g[a].insert(end(g[a]), begin(g[b]), end(g[b]));
            g[b].clear();
            dist_base[b] = -INF;
        }
    }
};

/**
 * Dinic?????????????????§???
 *
 * ?????¨???????????¬????????????
 * template?????°???int V???????????°
 * Edge??????rev?????????
 * ---
 * void add_edge(Graph<Edge> &g, int from, int to, int cap) {
 *     g[from].push_back(Edge{to, cap, (int)g[to].size()});
 *     g[to].push_back(Edge{from, 0, (int)g[from].size()-1});
 * }
 * ---
 */
template<class C, C INF> // Cap
struct MaxFlow {
    int V;
    vector<int> level, iter;
    //g?????´?£????????????§??¨???
    template<class E>
    C exec(Graph<E> &g, int s, int t) {    
        V = (int)g.size();
        level.resize(V);
        iter.resize(V);
        C flow = 0;
        while (true) {
            bfs(g, s);
            if (level[t] < 0) return flow;
            fill_n(iter.begin(), V, 0);
            while (true) {
                C f = dfs(g, s, t, INF);
                if (!f) break;
                flow += f;
            }
        }
    }

    template<class E>
    void bfs(const Graph<E> &g, int s) {
        fill_n(level.begin(), V, -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while (!que.empty()) {
            int v = que.front(); que.pop();
            for (E e: g[v]) {
                if (e.cap <= 0) continue;
                if (level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }

    template<class E>
    C dfs(Graph<E> &g, int v, int t, C f) {
        if (v == t) return f;
        C res = 0;
        for (int &i = iter[v]; i < (int)g[v].size(); i++) {
            E &e = g[v][i];
            if (e.cap <= 0) continue;
            if (level[v] < level[e.to]) {
                C d = dfs(g, e.to, t, min(f, e.cap));
                if (d <= 0) continue;
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                return d;
            }
        }
        return res;
    }
};

int rand_int(int l, int r) { //[l, r)
    using D = uniform_int_distribution<int>;
    static random_device rd;
    static mt19937 gen(rd());
    return D(l, r-1)(gen);
}
bool solve() {
    int n, m;
    cin >> n >> m;
    if (!n) return false;
    struct Edge {
        int to;
        ll dist;
    };
    Graph<Edge> g(n);
    auto addEdge = [&](int a, int b, ll c) {
        g[a].push_back(Edge{b, c});
        g[b].push_back(Edge{a, c});
    };
    ll s = 0;
    for (int i = 0; i < m; i++) {
        int a, b; ll c;
        cin >> a >> b >> c;
        if (c < 0) {
            s += c;
            continue;
        }
        addEdge(a, b, c);
    }
    cout << s+BidirectedCut<ll, TEN(15)>(g).sum << endl;
    return true;
}
 
int main() {
    while (solve()) {}
    return 0;
}