#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int id, v, type;
  long long c;
  Edge(int id, int v, long long c, int t) : id(id), v(v), c(c), type(t) {}
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<Edge>> g(n);
  vector<int> used(m, -1);
  vector<int> w(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    g[a].emplace_back(i, b, c, 0);
    g[b].emplace_back(i, a, c, 1);
    w[a] += c;
    w[b] += c;
  }
  queue<int> q;
  q.push(0);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto E : g[u]) {
      if (used[E.id] != -1) continue;
      used[E.id] = E.type;
      w[E.v] -= 2 * E.c;
      if (w[E.v] == 0 and E.v + 1 != n) {
        q.push(E.v);
      }
    }
  }
  for (int i = 0; i < m; ++i) cout << used[i] << '\n';
}
