#include <bits/stdc++.h>
using namespace std;
const int N = 200011;
int F[N];
int C[N];
int T[N];
int dir[N];
vector<pair<int, int> > adj[N];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    F[a] += c;
    F[b] += c;
    adj[a].emplace_back(b, i);
    adj[b].emplace_back(a, i);
    T[i] = a;
    C[i] = c;
  }
  for (int i = 1; i < n - 1; i++) F[i] >>= 1;
  queue<int> Q;
  F[0] = 0;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (auto ed : adj[v]) {
      int u = ed.first;
      int id = ed.second;
      if (dir[id]) continue;
      if (T[id] == v)
        dir[id] = -1;
      else
        dir[id] = 1;
      F[u] -= C[id];
      if (F[u] == 0) Q.push(u);
    }
  }
  for (int i = 0; i < m; i++) cout << max(0, dir[i]) << endl;
  return 0;
}
