#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> ans;
vector<int> cap;
vector<bool> done;
vector<vector<int>> g;
vector<tuple<int, int, int>> edges;
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  g.resize(n + 1);
  edges.resize(m);
  ans.resize(m);
  cap.resize(n + 1);
  done.resize(m);
  for (int i = 0; i < m; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    edges[i] = make_tuple(u, v, c);
    g[u].emplace_back(i);
    g[v].emplace_back(i);
    cap[u] += c;
    cap[v] += c;
  }
  priority_queue<pair<int, int>> pq;
  pq.emplace(0, 1);
  while (pq.size()) {
    auto [_, u] = pq.top();
    pq.pop();
    if (_ != 0) continue;
    for (auto e : g[u]) {
      if (not done[e]) {
        done[e] = true;
        auto [x, y, c] = edges[e];
        int v = x + y - u;
        ans[e] = (u == y);
        if (v == n) continue;
        cap[v] -= 2 * c;
        pq.emplace(-cap[v], v);
      }
    }
  }
  for (auto x : ans) cout << x << '\n';
}
