#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
priority_queue<pair<int, int> > q;
int n, m, sz, lim[N], a[N], b[N];
bool v[N];
vector<pair<int, int> > p[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int c;
    scanf("%d%d%d", a + i, b + i, &c);
    --a[i], --b[i];
    p[b[i]].push_back(make_pair(a[i], c));
    p[a[i]].push_back(make_pair(b[i], c));
    lim[a[i]] += c;
    lim[b[i]] += c;
  }
  lim[0] = 0;
  for (int i = 0; i < n; ++i) q.push(make_pair(-lim[i], i));
  v[--n] = true;
  lim[n] = n;
  while (sz < n) {
    int t = q.top().second;
    q.pop();
    if (v[t]) continue;
    lim[t] = sz++;
    v[t] = true;
    for (int i = 0; i < p[t].size(); ++i) {
      if (v[p[t][i].first]) continue;
      lim[p[t][i].first] -= p[t][i].second * 2;
      q.push(make_pair(-lim[p[t][i].first], p[t][i].first));
    }
  }
  for (int i = 0; i < m; ++i) puts(lim[a[i]] > lim[b[i]] ? "1" : "0");
  return 0;
}
