#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int C = 26;
void add(int &x, int y) {
  x += y;
  while (x >= MOD) x -= MOD;
  while (x < 0) x += MOD;
}
int fix(int x) {
  while (x >= MOD) x -= MOD;
  while (x < 0) x += MOD;
  return x;
}
int pw(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % MOD;
    b >>= 1;
    a = 1ll * a * a % MOD;
  }
  return ret;
}
const int MAXN = 2e5 + 10;
const int INF = 2e9 + 5;
int n, m, vec[MAXN], ans[MAXN], qu[MAXN], sz;
vector<array<int, 4>> g[MAXN];
void solve() {
  cin >> n >> m;
  memset(ans, -1, 4 * m);
  for (int i = 0, u, v, w; i < m; i++) {
    cin >> u >> v >> w;
    --u, --v;
    g[u].push_back({v, w, i, 0});
    g[v].push_back({u, w, i, 1});
    vec[u] += w;
    vec[v] += w;
  }
  for (int i = 1; i < n - 1; i++) {
    vec[i] /= 2;
  }
  vec[0] = 0;
  vec[n - 1] = INF;
  qu[sz++] = 0;
  for (int i = 0; i < sz; i++) {
    int u = qu[i];
    for (auto [v, w, j, dir] : g[u]) {
      if (~ans[j]) continue;
      vec[v] -= w;
      ans[j] = dir;
      if (vec[v] == 0 && v != n - 1) qu[sz++] = v;
    }
  }
  for (int i = 0; i < m; i++) {
    assert(~ans[i]);
    cout << ans[i] << "\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int te = 1;
  for (int w = 1; w <= te; w++) {
    solve();
  }
  return 0;
}
