#include <bits/stdc++.h>
using namespace std;
struct node {
  int to, next, va, dir;
} g[500000];
queue<int> q;
int ed, st[220000];
inline void add(int x, int y, int z) {
  ed++;
  g[ed].to = y;
  g[ed].next = st[x];
  g[ed].va = z;
  g[ed].dir = 0;
  st[x] = ed;
}
int n, m, x1, x2, x3, vis[220000], ind[220000], all[220000];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &x1, &x2, &x3);
    add(x1, x2, x3);
    add(x2, x1, x3);
    all[x1] += x3;
    all[x2] += x3;
  }
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    x1 = q.front();
    q.pop();
    for (int i = st[x1]; i != 0; i = g[i].next) {
      if (vis[g[i].to] == 0) {
        ind[g[i].to] += g[i].va;
        g[i].dir = 1;
      }
    }
    for (int i = st[x1]; i != 0; i = g[i].next) {
      if (g[i].to != n && vis[g[i].to] == 0 &&
          ind[g[i].to] * 2 == all[g[i].to]) {
        vis[g[i].to] = 1;
        q.push(g[i].to);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    printf("%d\n", g[i * 2].dir);
  }
  return 0;
}
