#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, c, i, d;
  edge(int v, int c, int i, int d) : v(v), c(c), i(i), d(d) {}
};
int n, m, d[200005], f[200005];
vector<edge> G[200005];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a--;
    b--;
    G[a].push_back(edge(b, c, i, 0));
    G[b].push_back(edge(a, c, i, 1));
    f[a] += c;
    f[b] += c;
  }
  for (int i = 0; i < n; i++) f[i] /= 2;
  memset(d, -1, sizeof(d));
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (edge& e : G[u]) {
      int id = e.i;
      if (d[id] == -1) {
        d[id] = e.d;
        int v = e.v;
        f[v] -= e.c;
        if (v != n - 1 && f[v] == 0) {
          Q.push(v);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", d[i]);
}
