#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
struct edge {
  int to, cost, id, dir;
};
int n, m, in[N], out[N], vis[N], ans[N];
vector<edge> G[N];
void bfs() {
  queue<int> que;
  vis[1] = 1, que.push(1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (edge &e : G[v]) {
      int u = e.to;
      if (vis[u]) continue;
      in[u] += e.cost;
      ans[e.id] = e.dir;
      if (in[u] == out[u] && u != n) vis[u] = 1, que.push(u);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    G[u].push_back({v, w, i, 0});
    G[v].push_back({u, w, i, 1});
    out[u] += w, out[v] += w;
  }
  for (int i = 2; i < n; i++) out[i] = out[i] / 2;
  bfs();
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
