#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b, c, i, dir;
  void in(int _i) {
    i = _i;
    cin >> a >> b >> c;
    dir = 0;
  }
};
vector<edge> v[200047];
vector<int> q;
bool vis[200047];
int sum[200047];
int zalyly[200047];
int ans[200047];
int main() {
  int n, m;
  cin >> n >> m;
  int ai, bi, ci;
  memset(vis, 0, sizeof(vis));
  memset(sum, 0, sizeof(sum));
  memset(zalyly, 0, sizeof(zalyly));
  for (int i = 0; i < m; i++) {
    edge e;
    e.in(i);
    v[e.a].push_back(e);
    swap(e.a, e.b);
    e.dir = 1;
    v[e.a].push_back(e);
    sum[e.a] += e.c;
    sum[e.b] += e.c;
  }
  q.push_back(1);
  vis[1] = 1;
  int cur;
  edge cur_e;
  while (q.size()) {
    cur = q.back();
    q.pop_back();
    for (int i = 0; i < v[cur].size(); i++) {
      cur_e = v[cur][i];
      if (vis[cur_e.b]) continue;
      ans[cur_e.i] = cur_e.dir;
      zalyly[cur_e.b] += cur_e.c;
      if (cur_e.b == n) continue;
      if (2 * zalyly[cur_e.b] == sum[cur_e.b]) {
        q.push_back(cur_e.b);
        vis[cur_e.b] = 1;
      }
    }
  }
  for (int i = 0; i < m; i++) cout << ans[i] << endl;
  return 0;
}
