#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to;
  int next;
  int dir;
  int flow;
  int num;
} e[200010 << 1];
int box[200010], cnt;
void init() {
  memset(box, -1, sizeof(box));
  cnt = 0;
}
void add(int from, int to, int flow, int dir, int num) {
  e[cnt].to = to;
  e[cnt].flow = flow;
  e[cnt].next = box[from];
  e[cnt].dir = dir;
  e[cnt].num = num;
  box[from] = cnt++;
}
int f[200010], ans[200010];
void solve(int n) {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    int t, v, dir, flow, num;
    for (t = box[now]; t + 1; t = e[t].next) {
      v = e[t].to, dir = e[t].dir, num = e[t].num, flow = e[t].flow;
      if (ans[num] == -1) {
        ans[num] = dir;
        f[v] -= flow;
        if (f[v] == 0 && v != n) {
          q.push(v);
        }
      }
    }
  }
}
int main() {
  int n, m, i, a, b, c;
  memset(ans, -1, sizeof(ans));
  memset(f, 0, sizeof(f));
  scanf("%d%d", &n, &m);
  init();
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c, 0, i);
    add(b, a, c, 1, i);
    f[a] += c;
    f[b] += c;
  }
  for (i = 1; i <= n; i++) {
    f[i] /= 2;
  }
  solve(n);
  for (i = 0; i < m; i++) printf("%d\n", ans[i]);
  return 0;
}
