#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
struct coord {
  int v, er, t, in;
};
queue<int> q;
coord c;
int n, m, i, v, to, t[N], s[N], f[N], er[N], gum[N], h[N];
bool mark[N];
vector<coord> graph[N];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; ++i) {
    scanf("%d%d%d", &s[i], &f[i], &er[i]);
    c.v = f[i];
    c.er = er[i];
    c.t = 0;
    c.in = i;
    graph[s[i]].push_back(c);
    c.v = s[i];
    c.er = er[i];
    c.t = 1;
    c.in = i;
    graph[f[i]].push_back(c);
    gum[s[i]] += er[i];
    gum[f[i]] += er[i];
  }
  q.push(n);
  mark[n] = 1;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    for (i = 0; i < graph[v].size(); ++i) {
      to = graph[v][i].v;
      if (mark[to] == 0 && to != 1) {
        gum[to] -= graph[v][i].er;
        h[to] += graph[v][i].er;
        t[graph[v][i].in] = 1 - graph[v][i].t;
        if (gum[to] == h[to]) {
          mark[to] = 1;
          q.push(to);
        }
      }
    }
  }
  for (i = 1; i <= m; ++i) {
    if (s[i] == 1 || f[i] == 1) {
      if (s[i] == 1) {
        printf("0\n");
      } else
        printf("1\n");
    } else {
      printf("%d\n", t[i]);
    }
  }
  return 0;
}
