#include <bits/stdc++.h>
double eps = 1e-9;
using namespace std;
const int maxn = 200002;
struct edge {
  int v, c, id, d;
};
int d[maxn], f[maxn];
vector<vector<edge> > graph;
int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  memset(d, -1, sizeof(d));
  graph.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    f[a] += c;
    f[b] += c;
    graph[a].push_back({b, c, i, 0});
    graph[b].push_back({a, c, i, 1});
  }
  for (int i = 0; i < n; i++) {
    f[i] /= 2;
  }
  queue<int> q;
  q.push(0);
  while (q.empty() == false) {
    int fronts = q.front();
    q.pop();
    for (int i = 0; i < graph[fronts].size(); i++) {
      edge tup = graph[fronts][i];
      if (d[tup.id] == -1) {
        d[tup.id] = tup.d;
        f[tup.v] -= tup.c;
        if (f[tup.v] == 0 && tup.v != n - 1) {
          q.push(tup.v);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    cout << d[i] << "\n";
  }
  return 0;
}
