#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int inf = 1e9;
int mod = 1e9 + 7;
struct edge {
  int u, v, c;
  int dir(int x) {
    if (x == u) {
      return 0;
    }
    return 1;
  }
  int other(int x) { return x == u ? v : u; }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<edge> E;
  vector<int> sum(n + 1, 0);
  vector<vector<int>> graph(n + 1);
  vector<int> cnt(m, -1);
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    edge temp;
    temp.u = u;
    temp.v = v;
    temp.c = c;
    E.push_back(temp);
    graph[u].push_back(i);
    graph[v].push_back(i);
    sum[u] += c;
    sum[v] += c;
  }
  for (int i = 1; i <= n; i++) {
    sum[i] /= 2;
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    for (int u : graph[x]) {
      if (cnt[u] == -1) {
        cnt[u] = E[u].dir(x);
        sum[E[u].other(x)] -= E[u].c;
        if (sum[E[u].other(x)] == 0 && E[u].other(x) != n) {
          q.push(E[u].other(x));
        }
      }
    }
    q.pop();
  }
  for (int i = 0; i < m; i++) {
    cout << cnt[i] << "\n";
  }
  return 0;
}
