#include <bits/stdc++.h>
const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const long long MXLL = 4611686018427387903;
const int Sz = 1110111;
using namespace std;
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
}
int n, m;
int a[Sz], b[Sz], c[Sz];
int ans[Sz];
vector<pair<int, int> > g[Sz];
map<pair<int, int>, pair<int, int> > ind;
int f[Sz];
int main() {
  Read_rap();
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    g[a[i]].push_back(make_pair(b[i], c[i]));
    g[b[i]].push_back(make_pair(a[i], c[i]));
    f[a[i]] += c[i];
    f[b[i]] += c[i];
    ind[make_pair(a[i], b[i])] = make_pair(i, 0);
    ind[make_pair(b[i], a[i])] = make_pair(i, 1);
  }
  f[1] = 0;
  for (int i = 2; i < n; i++) f[i] >>= 1;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto e : g[v]) {
      int to = e.first, len = e.second;
      if (f[to]) {
        ans[ind[make_pair(v, to)].first] = ind[make_pair(v, to)].second;
        f[to] -= len;
        if (f[to] == 0) q.push(to);
      }
    }
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << endl;
  return 0;
}
