#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
const int mod = (int)1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
int n, m, k;
struct node {
  int v, w, next, f;
} edg[400010];
int head[200010], en;
void add(int u, int v, int w) {
  edg[en].v = v;
  edg[en].w = w;
  edg[en].next = head[u];
  head[u] = en++;
}
int dp[200010];
void bfs() {
  for (int i = 2; i <= n - 1; i++) dp[i] /= 2;
  queue<int> q;
  q.push(1);
  int u, v;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = edg[i].next) {
      v = edg[i].v;
      if (edg[i].f == 0) {
        dp[v] -= edg[i].w;
        edg[i].f = 1;
        edg[i ^ 1].f = 2;
        if (dp[v] == 0 && v != n) {
          q.push(v);
        }
      }
    }
  }
  for (int i = 0; i < en; i += 2) {
    if (edg[i].f == 1) {
      puts("0");
    } else
      puts("1");
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    en = 0;
    memset(head, -1, sizeof head);
    int u, v, w;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &u, &v, &w);
      add(u, v, w);
      add(v, u, w);
      dp[u] += w;
      dp[v] += w;
    }
    bfs();
  }
  return 0;
}
