#include <bits/stdc++.h>
using namespace std;
inline long long ma(long long a, long long b) { return a > b ? a : b; }
inline long long mi(long long a, long long b) { return a > b ? b : a; }
struct Edge {
  long long v, w, id, d;
  Edge() {}
  Edge(long long v, long long w, long long id, long long d)
      : v(v), w(w), id(id), d(d) {}
};
long long f[300006], froz[300006];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  vector<Edge> adj[300006];
  for (long long(i) = (0); (i) < (m); (i)++) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    Edge ans;
    ans.v = b, ans.w = c, ans.id = i;
    ans.d = 0;
    adj[a].push_back(ans);
    f[a] += c;
    f[b] += c;
    ans.v = a, ans.d = 1;
    adj[b].push_back(ans);
  }
  for (long long(i) = (0); (i) < (n); (i)++) f[i] /= 2;
  queue<long long> q;
  q.push(0);
  memset(froz, -1, sizeof(froz));
  while (!q.empty()) {
    long long nod = q.front();
    q.pop();
    for (auto xd : adj[nod]) {
      if (froz[xd.id] == -1) {
        froz[xd.id] = xd.d;
        f[xd.v] -= xd.w;
        if (f[xd.v] == 0 && xd.v != n - 1) q.push(xd.v);
      }
    }
  }
  for (long long(i) = (0); (i) < (m); (i)++) cout << froz[i] << endl;
  return 0;
}
