#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  long long n;
  cin >> n;
  long long m;
  cin >> m;
  std::vector<std::set<std::pair<long long, long long> > > adj(n);
  std::vector<long long> to(n, 0);
  std::map<long long, long long> ma;
  for (long long i = 0; i < m; ++i) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    ma[a * 3ll * 100000ll + b] = (i + 1);
    ma[b * 3ll * 100000ll + a] = 0;
    adj[a].insert(make_pair(b, c));
    adj[b].insert(make_pair(a, c));
    to[a] += c;
    to[b] += c;
  }
  0;
  for (auto p : adj[0]) {
    long long v, w;
    tie(v, w) = p;
    to[v] -= (2ll * w);
    adj[v].erase(make_pair(0, w));
  }
  to[n - 1] *= 2;
  std::set<std::pair<long long, long long> > s;
  for (long long i = 1; i < n; ++i) s.insert(make_pair(to[i], i));
  while (!s.empty()) {
    0;
    auto p = *(s.begin());
    s.erase(s.begin());
    long long u = p.second;
    if (p.first && u != n - 1) {
      cout << p.first << " " << u << "\n";
      exit(0);
    }
    for (auto p1 : adj[u]) {
      long long v, w;
      tie(v, w) = p1;
      s.erase(make_pair(to[v], v));
      to[v] -= (2ll * w);
      s.insert(make_pair(to[v], v));
      adj[v].erase(make_pair(u, w));
    }
  }
  std::vector<long long> res(m + 1, -1);
  for (long long u = 0; u < n; ++u) {
    for (auto p : adj[u]) {
      long long v, w;
      tie(v, w) = p;
      if (ma[u * 3ll * 100000ll + v] == 0)
        res[ma[v * 3ll * 100000ll + u]] = 1;
      else
        res[ma[u * 3ll * 100000ll + v]] = 0;
    }
  }
  for (long long i = 0; i < m; ++i) cout << res[i + 1] << "\n";
}
