#include <bits/stdc++.h>
using namespace std;
mt19937_64 random_num(
    chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const int base = 26;
int n, m;
struct edge {
  int v, c, id, type;
};
int inflow[200001];
vector<edge> ke[200001];
queue<int> q;
int res[200001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    ke[u].push_back({v, c, i, 0});
    ke[v].push_back({u, c, i, 1});
    inflow[u] += c;
    inflow[v] += c;
    res[i] = -1;
  }
  for (int i = 1; i <= n; i++) inflow[i] /= 2;
  inflow[1] = 0;
  q.push(1);
  while (q.empty() == 0) {
    int u = q.front();
    q.pop();
    for (auto t : ke[u]) {
      int id = t.id, v = t.v, c = t.c, type = t.type;
      if (res[id] == -1) {
        inflow[v] -= c;
        res[id] = type;
        if (inflow[v] == 0 && v != n) q.push(v);
      }
    }
  }
  for (int i = 1; i <= m; i++) cout << res[i] << "\n";
}
