#include <bits/stdc++.h>
using namespace std;
unordered_map<
    long long,
    unordered_map<long long, pair<pair<long long, long long>, long long> > >
    adj;
vector<long long> in, total, ans;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, a, b, c;
  cin >> n >> m;
  in.assign(n + 1, 0);
  total.assign(n + 1, 0);
  ans.assign(m, 0);
  for (long long i = 0; i < m; i++) {
    cin >> a >> b >> c;
    total[a] += c;
    total[b] += c;
    adj[a][b] = {{c, 0}, i};
    adj[b][a] = {{c, 1}, i};
  }
  queue<long long> bfs;
  bfs.push(1);
  while (bfs.size()) {
    long long top = bfs.front();
    bfs.pop();
    for (auto &i : adj[top]) {
      ans[i.second.second] = i.second.first.second;
      in[i.first] += i.second.first.first;
      if ((i.first != n) && 2 * in[i.first] == total[i.first])
        bfs.push(i.first);
      adj[i.first].erase(top);
    }
  }
  for (long long i = 0; i < m; i++) cout << ans[i] << '\n';
  return 0;
}
