Flawed Flow

Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.

However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.

More formally. You are given an undirected graph. For each it’s undirected edge ( a ib i) you are given the flow volume c i. You should direct all edges in such way that the following conditions hold:

  1. for each vertex v (1 < v < n), sum of c i of incoming edges is equal to the sum of c i of outcoming edges;
  2. vertex with number 1 has no incoming edges;
  3. the obtained directed graph does not have cycles.

Input

The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers a ib i and c i (1 ≤ a i, b i ≤ na i ≠ b i, 1 ≤ c i ≤ 104), which means that there is an undirected edge from a i to b i with flow volume c i.

It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.

Output

Output m lines, each containing one integer d i, which should be 0 if the direction of the i-th edge is a i → b i (the flow goes from vertex a i to vertex b i) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input.

If there are several solutions you can print any of them.

Examples

input

3 3
3 2 10
1 2 10
3 1 5

output

1
0
1

input

4 5
1 2 10
1 3 10
2 3 5
4 2 15
3 4 5

output

0
0
1
1
0

Note

In the first test case, 10 flow units pass through path , and 5 flow units pass directly from source to sink: .

Solution:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>

using namespace std;

const int N = 888888;

int n, m, i, j, e;
int ss[N], ff[N], dd[N], x[N], last[N], pred[N], good[N], deg[N], used[N];

int main() {
scanf("%d %d", &n, &m);
for (i=1;i<=m;i++) {
    scanf("%d %d %d", ss+i, ff+i, dd+i);
    ss[i+m] = ff[i];
    ff[i+m] = ss[i];
    dd[i+m] = dd[i];
  }
  for (i=1;i<=n;i++) last[i] = 0;
  for (i=1;i<=m+m;i++) {
    good[i] = 0;
    pred[i] = last[ss[i]];
    last[ss[i]] = i;
  }
  for (i=1;i<=n;i++) deg[i] = 0;
  for (i=1;i<=m+m;i++) deg[ss[i]] += dd[i];
  for (i=1;i<=n;i++) deg[i] /= 2;
  for (i=1;i<=n;i++) used[i] = 0;
  deg[1] = deg[n] = 0;
  i = 1; e = 1;
  x[1] = 1;
  while (i <= e) {
    j = last[x[i]];
    while (j > 0) {
if (ff[j] == n) good[j] = 1;
if (used[ff[j]] < deg[ff[j]]) {
        good[j] = 1;
        used[ff[j]] += dd[j];
        if (used[ff[j]] == deg[ff[j]]) {
          e++;
          x[e] = ff[j];
        }
      }
      j = pred[j];
    }
    i++;
  }
  for (i=1;i<=m;i++) printf("%d\n", 1-good[i]);
  return 0;
}