#include <bits/stdc++.h>
using namespace std;
struct edge {
  int64_t u, v, use, fl;
};
vector<vector<int64_t>> g;
vector<edge> ed;
vector<int64_t> flo;
void dfs(int64_t v) {
  for (auto ch : g[v]) {
    if (ed[ch].use != -1) continue;
    ed[ch].use = 0;
    if (ed[ch].v == v) {
      swap(ed[ch].v, ed[ch].u);
      ed[ch].use = 1;
    }
    flo[ed[ch].v] -= ed[ch].fl;
    if (flo[ed[ch].v] == 0) dfs(ed[ch].v);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int64_t n, m;
  cin >> n >> m;
  g.clear();
  g.assign(n, vector<int64_t>());
  ed.clear();
  flo.assign(n, 0);
  for (int64_t i = 0; i < m; i++) {
    int64_t c, d, fl;
    cin >> c >> d >> fl;
    c--;
    d--;
    flo[c] += fl;
    flo[d] += fl;
    g[c].push_back(i);
    g[d].push_back(i);
    ed.push_back({c, d, -1, fl});
  }
  for (int64_t i = 0; i < n; i++) {
    flo[i] /= 2;
  }
  flo[n - 1] = 100000000000;
  dfs(0);
  for (int64_t i = 0; i < m; i++) {
    cout << ed[i].use << '\n';
  }
}
