#include <bits/stdc++.h>
using namespace std;
const int MAXM = 4e5 + 5;
const int MAXN = 2e5 + 5;
struct pool {
  int v, pre, e;
} p[MAXM];
int head[MAXN], num, in[MAXN], out[MAXN], a[MAXM], b[MAXM], c[MAXM];
bool d[MAXN];
int n, m;
stack<int> s;
void add(int u, int v, int id) {
  p[++num].v = v;
  p[num].e = id;
  p[num].pre = head[u];
  head[u] = num;
}
void solve() {
  s.push(1);
  out[1] = 0;
  while (!s.empty()) {
    int u = s.top();
    s.pop();
    for (int tmp = head[u], v; v = p[tmp].v, tmp; tmp = p[tmp].pre) {
      if (in[v] == out[v] && v != n) continue;
      in[v] += c[p[tmp].e], out[v] -= c[p[tmp].e];
      if (in[v] == out[v] && v != n) s.push(v);
      if (v == a[p[tmp].e]) d[p[tmp].e] = true;
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  num = 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", a + i, b + i, c + i);
    out[a[i]] += c[i];
    out[b[i]] += c[i];
    add(a[i], b[i], i);
    add(b[i], a[i], i);
  }
  solve();
  for (int i = 1; i <= m; i++) printf("%d\n", d[i]);
}
