#include <bits/stdc++.h>
using namespace std;
struct edge {
  int b, l, c;
} g[200005 << 1];
int h[200005], t, r[200005], mark[200005 << 1];
queue<int> q;
void addedge(int a, int b, int c) {
  g[++t] = (edge){b, h[a], c};
  h[a] = t;
  g[++t] = (edge){a, h[b], c};
  h[b] = t;
  r[b] += c;
  r[a] += c;
}
int arc(int i) { return i + (i & 1 ? 1 : -1); }
int main() {
  int n, m, i, a, b, c;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    addedge(a, b, c);
  }
  for (i = 2; i < n; i++) r[i] /= 2;
  q.push(1);
  while (q.size()) {
    a = q.front();
    q.pop();
    for (i = h[a]; i; i = g[i].l)
      if (!mark[i] && !mark[arc(i)]) {
        r[g[i].b] -= g[i].c;
        mark[i] = 1;
        if (!r[g[i].b]) q.push(g[i].b);
      }
  }
  for (i = 1; i <= m; i++) printf("%d\n", mark[i << 1]);
  return 0;
}
