#include <bits/stdc++.h>
int dp, pre[1000010], p[1000010], tt[1000010], id[1000010], typ[1000010],
    ans[1000010];
int flow1[1000010], flow2[1000010], vis[1000010], z[1000010], ww[1000010];
int n, m, a, b, c, i, t, w;
void link(int x, int y, int v, int z, int w) {
  dp++;
  pre[dp] = p[x];
  p[x] = dp;
  tt[dp] = y;
  ww[dp] = v;
  id[dp] = z;
  typ[dp] = w;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    flow1[a] += c;
    flow1[b] += c;
    link(a, b, c, i, 0);
    link(b, a, c, i, 1);
  }
  t = 0;
  w = 1;
  z[w] = 1;
  while (t != w) {
    t++;
    i = p[z[t]];
    vis[z[t]] = 1;
    while (i) {
      if (((flow1[tt[i]] != flow2[tt[i]]) && (tt[i] != 1)) || (tt[i] == n)) {
        ans[id[i]] = typ[i];
        flow1[tt[i]] -= ww[i];
        flow2[tt[i]] += ww[i];
        if ((flow1[tt[i]] == flow2[tt[i]]) && (tt[i] != n)) {
          w++;
          z[w] = tt[i];
        }
      }
      i = pre[i];
    }
  }
  for (i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
