#include <bits/stdc++.h>
using namespace std;
long long c[200001], ans[200001], xd[200001];
bool vis[200001];
vector<pair<long long, long long> > v[200001];
int main() {
  long long n, i, a1, a2, m, s, t;
  memset(xd, 0, sizeof(xd));
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &a1, &a2, &c[i]);
    a1--, a2--;
    v[a1].push_back({a2, i});
    v[a2].push_back({a1, i});
    ans[i] = a1;
    xd[a1] += c[i];
    xd[a2] += c[i];
    if (a1 == n - 1)
      ans[i] = 1;
    else if (a2 == n - 1)
      ans[i] = 0;
  }
  memset(vis, 0, sizeof(vis));
  s = 0;
  t = n - 1;
  queue<long long> q;
  q.push(0);
  while (1) {
    if (q.empty()) break;
    long long zz = q.front();
    q.pop();
    vis[zz] = 1;
    for (auto pp : v[zz])
      if (!vis[pp.first] and pp.first != t) {
        xd[pp.first] -= (2 * c[pp.second]);
        ans[pp.second] = ((ans[pp.second] == zz) ? 0 : 1);
        if (xd[pp.first] == 0) q.push(pp.first);
      }
  }
  for (i = 0; i < m; i++) printf("%lld\n", ans[i]);
}
