#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int res[N * 2], n, m, tot[N];
struct Edge {
  int e, flow, num;
  bool dir;
  Edge(int _e, int _flow, int _num, bool _dir) {
    e = _e, flow = _flow, num = _num, dir = _dir;
  }
};
vector<Edge> V[N];
int solve() {
  queue<int> q;
  q.push(1);
  memset(res, -1, sizeof res);
  while (!q.empty()) {
    int deq = q.front();
    q.pop();
    for (__typeof((V[deq]).begin()) it = (V[deq]).begin(); it != (V[deq]).end();
         it++) {
      if (res[it->num] != -1) continue;
      tot[it->e] -= 2 * (it->flow);
      res[it->num] = it->dir;
      if (tot[it->e] == 0 && it->e != n) q.push(it->e);
    }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", res[i]);
  return 0;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    V[a].push_back(Edge(b, c, i, 0));
    V[b].push_back(Edge(a, c, i, 1));
    tot[a] += c;
    tot[b] += c;
  }
  return solve();
}
