#include <bits/stdc++.h>
using namespace std;
struct node {
  int from, to, next, w, dir, id;
} e[1500000];
int head[350000];
int in[350000];
int out[350000];
int ans[350000];
int n, m, cont;
void add(int from, int to, int w, int dir, int id) {
  e[cont].from = from;
  e[cont].to = to;
  e[cont].w = w;
  e[cont].dir = dir;
  e[cont].id = id;
  e[cont].next = head[from];
  head[from] = cont++;
}
void findFlow() {
  queue<int> s;
  s.push(1);
  in[1] = out[1] = 0;
  while (!s.empty()) {
    int u = s.front();
    s.pop();
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      int w = e[i].w;
      int dir = e[i].dir;
      if (v == 1 || (in[v] == out[v] && v != n)) continue;
      if (u == 1 || v == n) {
        if (u == 1) {
          ans[e[i].id] = dir;
          in[v] += w;
        } else {
          ans[e[i].id] = dir;
        }
      } else {
        if (in[v] != out[v]) {
          in[v] += w;
          ans[e[i].id] = dir;
        } else
          ans[e[i].id] = 1 - dir;
      }
      if (in[v] == out[v] && v != n) {
        s.push(v);
      }
    }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    cont = 0;
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= m; i++) {
      int start, end, weight;
      scanf("%d%d%d", &start, &end, &weight);
      add(start, end, weight, 0, i);
      add(end, start, weight, 1, i);
      out[end] += weight;
      out[start] += weight;
    }
    for (int i = 1; i <= n; i++) out[i] = out[i] / 2;
    findFlow();
  }
}
