#include <bits/stdc++.h>
using namespace std;
const int N = 200010, M = 400010;
int cnt = 1, fir[N], nxt[M], to[M], cap[M];
int n, m, in[N], vis[N], ans[N];
queue<int> q;
void addedge(int a, int b, int c) {
  nxt[++cnt] = fir[a];
  to[fir[a] = cnt] = b;
  cap[cnt] = c;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b, c; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    addedge(a, b, c);
    addedge(b, a, c);
    in[a] += c;
    in[b] += c;
  }
  for (int i = 2; i < n; i++) in[i] /= 2;
  q.push(1);
  in[1] = 0;
  vis[1] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = fir[x]; i; i = nxt[i])
      if (!vis[to[i]]) {
        in[to[i]] -= cap[i];
        ans[i / 2] = i % 2;
        if (in[to[i]] == 0) {
          q.push(to[i]);
          vis[to[i]] = 1;
        }
      }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
