#include <bits/stdc++.h>
using namespace std;
int n, m, flow[200100][2], dtion[200100], Eg[200100];
bool vis[200100];
queue<int> q;
struct node {
  int v, d, i;
  node *next;
} edge[200100 * 2];
node *cnt = &edge[0];
node *adj[200100];
void Addedge(int u, int v, int d, int i) {
  node *p = ++cnt;
  p->v = v;
  p->d = d;
  p->i = i;
  p->next = adj[u];
  adj[u] = p;
}
void Solve() {
  int u, v, d;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &Eg[i], &v, &d);
    Addedge(Eg[i], v, d, i);
    flow[Eg[i]][0] += d;
    Addedge(v, Eg[i], d, i);
    flow[v][0] += d;
  }
  q.push(1);
  vis[1] = true;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (node *p = adj[u]; p; p = p->next) {
      v = p->v;
      d = p->d;
      if (vis[v]) continue;
      dtion[p->i] = u;
      flow[v][1] += d;
      flow[v][0] -= d;
      if (v != n && flow[v][1] == flow[v][0]) {
        vis[v] = true;
        q.push(v);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (dtion[i] == Eg[i])
      printf("0\n");
    else
      printf("1\n");
  }
}
int main() {
  Solve();
  return 0;
}
