#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int head[N], nxt[N * 2], to[N * 2], cost[N * 2], dir[N * 2], ne = 0;
int f[N];
void addedge(int a, int b, int c, int d) {
  nxt[ne] = head[a];
  to[ne] = b;
  cost[ne] = c;
  dir[ne] = d;
  head[a] = ne++;
}
int ans[N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  memset(head, -1, sizeof head);
  int a, b, c, d;
  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &a, &b, &c);
    addedge(a, b, c, 0);
    addedge(b, a, c, 1);
    f[a] += c;
    f[b] += c;
  }
  memset(ans, -1, sizeof ans);
  for (int i = 1; i <= n; ++i) f[i] /= 2;
  queue<int> q;
  q.push(1);
  while (q.size()) {
    int idx = q.front();
    q.pop();
    for (int i = head[idx]; ~i; i = nxt[i]) {
      int id = i / 2;
      if (ans[id] == -1) {
        ans[id] = dir[i];
        f[to[i]] -= cost[i];
        if (to[i] != n && f[to[i]] == 0) q.push(to[i]);
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    printf("%d\n", ans[i]);
  }
}
