#include <bits/stdc++.h>
using namespace std;
int ans[200010];
int hh[200010], ecnt;
int flow[200010], in[200010], use[200010];
struct Edge {
  int v, w, id, ne;
};
Edge e[400010];
void addEdge(int u, int v, int w, int id) {
  e[ecnt].v = v;
  e[ecnt].w = w;
  e[ecnt].id = id;
  e[ecnt].ne = hh[u];
  hh[u] = ecnt++;
}
queue<int> q;
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) == 2) {
    memset(hh, -1, sizeof(hh));
    ecnt = 0;
    for (int i = 0; i < m; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      addEdge(u, v, w, i + 1);
      addEdge(v, u, w, -i - 1);
      flow[u] += w;
      flow[v] += w;
    }
    memset(in, 0, sizeof(in));
    memset(use, 0, sizeof(use));
    int maxflow = flow[1];
    while (!q.empty()) q.pop();
    q.push(1);
    use[1] = 1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = hh[u]; i != -1; i = e[i].ne) {
        int v = e[i].v, w = e[i].w;
        if (use[v]) continue;
        in[v] += w;
        flow[v] -= w;
        if (v != n && in[v] == flow[v]) {
          use[v] = 1;
          q.push(v);
        }
        if (e[i].id > 0)
          ans[e[i].id] = 0;
        else
          ans[-e[i].id] = 1;
      }
    }
    for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  }
  return 0;
}
