#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int v, w, next;
} e[2 * 200005];
int n, m, cnt;
int head[200005], d[200005], ans[200005];
void init() {
  cnt = 0;
  memset(head, -1, sizeof(head));
  memset(d, 0, sizeof(d));
  memset(ans, -1, sizeof(ans));
}
void add(int u, int v, int w) {
  e[cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt++;
}
void solve() {
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int j = head[u]; j != -1; j = e[j].next) {
      int v = e[j].v;
      if (ans[j >> 1] == -1) {
        d[v] -= 2 * e[j].w;
        ans[j >> 1] = 1 & j;
        if (d[v] == 0) Q.push(v);
      }
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
}
int main() {
  init();
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    add(u, v, w);
    d[u] += w;
    add(v, u, w);
    d[v] += w;
  }
  d[n] = 0x3f3f3f3f;
  solve();
  return 0;
}
