#include <bits/stdc++.h>
using namespace std;
const long long MAX = 5e5 + 20;
long long inf = (long long)1e9;
long long mod = (long long)1e9 + 9;
long long n, m, ans[MAX], passed[MAX], am[MAX];
vector<pair<long long, long long>> gr[MAX];
set<pair<long long, long long>> val;
map<pair<long long, long long>, long long> ind;
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    ind[{a, b}] = i;
    gr[a].push_back({b, w});
    gr[b].push_back({a, w});
    am[a] += w;
    am[b] += w;
  }
  for (int i = 1; i < n; i++) {
    val.insert({am[i], i});
  }
  long long start = 1;
  for (int i = 0; i < n - 1; i++) {
    val.erase({am[start], start});
    passed[start] = 1;
    for (auto u : gr[start]) {
      long long stop = u.first;
      long long w = u.second;
      if (passed[stop]) continue;
      long long use;
      if (ind[{start, stop}] == 0) {
        use = ind[{stop, start}];
        ans[use] = 1;
      } else {
        use = ind[{start, stop}];
        ans[use] = 0;
      }
      val.erase({am[stop], stop});
      am[stop] -= 2 * w;
      if (stop != n) val.insert({am[stop], stop});
    }
    if (val.size()) {
      start = (*val.begin()).second;
      if (am[start])
        while (1)
          ;
    }
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << "\n";
}
int main() {
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios::sync_with_stdio(0);
  long long q;
  q = 1;
  while (q--) solve();
  return 0;
}
