#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, i, j, d[200005], f[200005];
vector<vector<edge> > g;
int main() {
  cin >> n >> m;
  g = vector<vector<edge> >(n);
  for (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 (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 (i = 0; i < g[u].size(); i++) {
      int id = g[u][i].i, v;
      if (d[id] == -1) {
        d[id] = g[u][i].d;
        v = g[u][i].v;
        f[v] -= g[u][i].c;
        if (v != n - 1 && f[v] == 0) Q.push(v);
      }
    }
  }
  for (i = 0; i < m; i++) printf("%d\n", d[i]);
  return 0;
}
