#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, m, in[N], tot[N], a[N], b[N], c[N], d[N];
vector<int> e[N];
void bfs() {
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = (0); i < (((int)(e[u]).size())); ++i) {
      if (~d[e[u][i]]) continue;
      int id = e[u][i];
      int v = a[id] ^ b[id] ^ u;
      d[id] = v == a[id], in[v] += c[id];
      if ((v < n) && (in[v] << 1) == tot[v]) que.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (m); ++i) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    d[i] = -1, tot[a[i]] += c[i], tot[b[i]] += c[i];
    e[a[i]].push_back(i);
    e[b[i]].push_back(i);
  }
  bfs();
  for (int i = (0); i < (m); ++i) printf("%d\n", d[i]);
  return 0;
}
