#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 10;
stack<int> Q;
vector<pair<int, pair<int, int> > > E[N];
int t, n, m, size, f[N], d[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, a, b, c; i < m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    f[a] += c;
    f[b] += c;
    E[a].push_back(make_pair(size++, make_pair(b, c)));
    E[b].push_back(make_pair(size++, make_pair(a, c)));
  }
  fill(d, d + m, -1);
  for (int i = 2; i < n; ++i) f[i] >>= 1;
  Q.push(1);
  while (!Q.empty()) {
    t = Q.top();
    Q.pop();
    for (__typeof(E[t].begin()) itr = E[t].begin(); itr != E[t].end(); ++itr)
      if (d[itr->first >> 1] == -1) {
        d[itr->first >> 1] = (itr->first & 1);
        f[itr->second.first] -= itr->second.second;
        if (f[itr->second.first] == 0) Q.push(itr->second.first);
      }
  }
  for (int i = 0; i < m; ++i) printf("%d\n", d[i]);
}
