#include <bits/stdc++.h>
using namespace std;
int n, m;
int u, v, c;
vector<vector<int> > adj, cost, rev;
vector<bool> vis, ans;
vector<int> sm;
int main() {
  cin >> n >> m;
  adj.resize(n);
  cost.resize(n);
  rev.resize(n);
  sm.resize(n);
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> c;
    u--, v--;
    sm[u] += c;
    sm[v] += c;
    adj[u].push_back(v);
    adj[v].push_back(u);
    cost[u].push_back(c);
    cost[v].push_back(c);
    rev[u].push_back((i + 1));
    rev[v].push_back(-(i + 1));
  }
  vis.resize(n, 0);
  ans.resize(m, 0);
  sm[0] = 0;
  for (int i = 0; i < n - 1; i++) sm[i] /= 2;
  queue<int> q;
  q.push(0);
  vis[0] = 1;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < (int)adj[cur].size(); i++) {
      int nxt = adj[cur][i];
      if (vis[nxt]) continue;
      int re = rev[cur][i];
      c = cost[cur][i];
      if (re < 0) {
        re *= -1;
        re--;
        ans[re] = 1;
      }
      sm[nxt] -= c;
      if (sm[nxt] == 0) {
        q.push(nxt);
        vis[nxt] = 1;
      }
    }
  }
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
}
