#include <bits/stdc++.h>
using namespace std;
int n, m, ans[200001], a, b, c, s[200001], q[200001], used[200001];
vector<pair<pair<int, int>, pair<int, int> > > g[200001];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    g[a].push_back(make_pair(make_pair(b, i), make_pair(c, 0)));
    g[b].push_back(make_pair(make_pair(a, i), make_pair(c, 1)));
    s[a] += c;
    s[b] += c;
  }
  used[1] = 1;
  int l = 0, r = 1;
  q[r] = 1;
  while (l < r) {
    int v = q[++l];
    for (int i = 0; i < g[v].size(); ++i) {
      int to = g[v][i].first.first, cap = g[v][i].second.first;
      if (used[to]) continue;
      s[to] -= 2 * cap;
      ans[g[v][i].first.second] = g[v][i].second.second;
      if (!s[to] && to != n) {
        q[++r] = to;
        used[to] = 1;
      }
    }
  }
  for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
}
