#include <bits/stdc++.h>
using namespace std;
const long long sz = 2e5 + 10;
struct node {
  long long v, flow, id, dir;
};
vector<node> g[sz];
long long flow[sz];
bool dir[sz], vis[sz];
int main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= m; ++i) {
    long long u, v, c;
    scanf("%lld", &u), scanf("%lld", &v), scanf("%lld", &c);
    g[u].push_back({v, c, i, 0});
    g[v].push_back({u, c, i, 1});
    flow[u] += c, flow[v] += c;
  }
  for (long long i = 2; i < n; i++) flow[i] /= 2;
  queue<long long> q;
  q.push(1);
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (node &nd : g[u]) {
      if (vis[nd.id]) continue;
      vis[nd.id] = 1, dir[nd.id] = nd.dir;
      flow[nd.v] -= nd.flow;
      if (nd.v != n && flow[nd.v] == 0) q.push(nd.v);
    }
  }
  for (long long i = 1; i <= m; ++i) printf("%d\n", (int)dir[i]);
  return 0;
}
