#include <bits/stdc++.h>
using namespace std;
struct {
  int p, c, id, dis, next;
} e[400001];
int g[200001], q[200001], ans[200001], d[200001], tot;
bool v[200001];
void addedge(int a, int b, int c, int d, int f) {
  e[tot].p = b;
  e[tot].c = c;
  e[tot].id = d;
  e[tot].dis = f;
  e[tot].next = g[a];
  g[a] = tot++;
}
int main() {
  memset(g, 0, sizeof(g));
  tot = 1;
  memset(v, false, sizeof(v));
  memset(d, 0, sizeof(d));
  int n, m, a, b, c, head = 1, tail = 1;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    addedge(a, b, c, i, 0);
    addedge(b, a, c, i, 1);
    d[a] += c;
    d[b] += c;
  }
  for (q[1] = 1, v[1] = true; head <= tail; head++)
    for (int tmp = g[q[head]]; tmp; tmp = e[tmp].next)
      if (!v[e[tmp].p]) {
        d[e[tmp].p] -= 2 * e[tmp].c;
        ans[e[tmp].id] = e[tmp].dis;
        if (!d[e[tmp].p] && e[tmp].p != n) v[q[++tail] = e[tmp].p] = true;
      }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
