#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
vector<pair<int, int>> g[N];
vector<pair<int, int>> edges;
set<pair<int, int>> found_edges;
map<pair<int, int>, bool> directed;
long long flow[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    edges.emplace_back(u, v);
    found_edges.insert({u, v});
    flow[u] += c;
    flow[v] += c;
    g[u].emplace_back(v, c);
    g[v].emplace_back(u, c);
  }
  for (int i = 1; i <= n; ++i) flow[i] /= 2;
  flow[1] = 0;
  queue<int> q;
  q.push(1);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto &it : g[u]) {
      int v = it.first;
      int c = it.second;
      if (directed.find({u, v}) == directed.end() &&
          directed.find({v, u}) == directed.end()) {
        if (found_edges.find({u, v}) != found_edges.end())
          directed[{u, v}] = 0;
        else
          directed[{v, u}] = 1;
        flow[it.first] -= it.second;
        if (!flow[it.first] && it.first != n) q.push(it.first);
      }
    }
  }
  for (auto &it : edges) cout << directed[it] << '\n';
  return 0;
}
