#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
vector<int> edge[200007], cost[200007];
vector<pair<int, int> > ind[200007];
int sum[200007], flow[200007], ans[200007], vis[200007];
queue<int> q;
int main() {
  int n, m, u, v, w, i;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d %d %d", &u, &v, &w);
    sum[u] += w;
    sum[v] += w;
    edge[u].push_back(v);
    cost[u].push_back(w);
    ind[u].push_back({i, 0});
    edge[v].push_back(u);
    cost[v].push_back(w);
    ind[v].push_back({i, 1});
  }
  q.push(1);
  vis[1] = 1;
  while (q.size()) {
    u = q.front();
    q.pop();
    ;
    for (i = 0; i < edge[u].size(); i++) {
      v = edge[u][i];
      if (vis[v]) continue;
      flow[v] += cost[u][i];
      ans[ind[u][i].first] = ind[u][i].second;
      if (v == n) continue;
      if (flow[v] == sum[v] / 2) {
        vis[v] = 1;
        q.push(v);
      }
    }
  }
  for (i = 1; i <= m; i++) cout << ans[i] << endl;
  return 0;
}
