#include <bits/stdc++.h>
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
using namespace std;
int flow[MAXN], dp[MAXN];
vector<pair<pair<int, int>, pair<int, int> > > adj[MAXN];
int main() {
  int n, m;
  scanf("%d", &(n)), scanf("%d", &(m));
  for (int i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d", &(u)), scanf("%d", &(v)), scanf("%d", &(c));
    u--, v--;
    adj[u].push_back({{v, c}, {i, 0}});
    adj[v].push_back({{u, c}, {i, 1}});
    flow[u] += c, flow[v] += c;
  }
  for (int i = 0; i < n; i++) flow[i] /= 2;
  memset(dp, -1, sizeof dp);
  queue<int> pq;
  pq.push(0);
  while (!pq.empty()) {
    int cur = pq.front();
    pq.pop();
    for (auto nx : adj[cur]) {
      int v = nx.first.first, c = nx.first.second;
      if (dp[nx.second.first] != -1) continue;
      dp[nx.second.first] = nx.second.second;
      flow[v] -= c;
      if (v != n - 1 && flow[v] == 0) pq.push(v);
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", (dp[i]));
  return 0;
}
