#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int total[N];
vector<pair<pair<int, int>, int> > e[N];
int dir[N], from[N], to[N];
int visit[N];
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  memset(total, 0, sizeof(total));
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    from[i] = u;
    to[i] = v;
    e[u].push_back(make_pair(make_pair(v, w), i));
    e[v].push_back(make_pair(make_pair(u, w), i));
    total[u] += w;
    total[v] += w;
    dir[i] = -1;
  }
  for (int i = 1; i <= n; i++) total[i] >>= 1;
  memset(visit, false, sizeof(visit));
  queue<int> q;
  q.push(1);
  visit[1] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < e[u].size(); i++) {
      int id = e[u][i].second, v = e[u][i].first.first,
          w = e[u][i].first.second;
      if (dir[id] == -1) {
        if (u == from[id])
          dir[id] = 0;
        else
          dir[id] = 1;
        total[v] -= w;
        if (total[v] == 0 && (!visit[v]) && v != n) {
          visit[v] = true;
          q.push(v);
        }
      }
    }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", dir[i]);
  return 0;
}
