#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int u, v, f, nxt;
  int dir;
  Edge(int u0 = 0, int v0 = 0, int f0 = 0, int nxt0 = 0) {
    u = u0;
    v = v0;
    f = f0;
    nxt = nxt0;
    dir = -1;
  }
} edge[500005];
int head[200005], ff[200005];
int cnt;
int n, m;
void init() {
  memset(ff, 0, sizeof(ff));
  cnt = 0;
  memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w) {
  edge[cnt] = Edge(u, v, w, head[u]);
  head[u] = cnt++;
}
void input() {
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    addEdge(u, v, w);
    addEdge(v, u, w);
    ff[u] += w;
    ff[v] += w;
  }
}
void compute() {
  for (int i = 2; i < n; i++) {
    ff[i] = ff[i] / 2;
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = head[cur]; i != -1; i = edge[i].nxt) {
      int nxt = edge[i].v;
      if (edge[i].dir == -1) {
        edge[i].dir = 0;
        edge[i ^ 1].dir = 1;
        ff[nxt] -= edge[i].f;
        if (ff[nxt] == 0) {
          q.push(nxt);
        }
      }
    }
  }
  for (int i = 0; i < cnt; i += 2) {
    cout << edge[i].dir << endl;
  }
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    init();
    input();
    compute();
  }
  return 0;
}
