#include <bits/stdc++.h>
using namespace std;
const int maxn = 300000 + 5;
struct Edge {
  int from, to, flow, dir;
  Edge(int from, int to, int flow, int dir)
      : from(from), to(to), flow(flow), dir(dir) {}
};
vector<Edge> edge;
vector<int> g[maxn];
int in[maxn], deg[maxn];
void addedge(int from, int to, int flow) {
  edge.push_back(Edge(from, to, flow, -1));
  int m = edge.size();
  g[from].push_back(m - 1);
  g[to].push_back(m - 1);
  deg[from] += flow;
  deg[to] += flow;
}
int n;
void topo() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      Edge &e = edge[g[u][i]];
      if (e.dir == -1) {
        int v;
        if (e.from == u)
          e.dir = 0, v = e.to;
        else
          e.dir = 1, v = e.from;
        in[v] += e.flow;
        if (in[v] == deg[v] && v != n) q.push(v);
      }
    }
  }
}
int main() {
  int m, u, v, f;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &f);
    addedge(u, v, f);
  }
  for (int i = 1; i <= n; i++) deg[i] /= 2;
  topo();
  for (int i = 0; i < m; i++) printf("%d\n", edge[i].dir);
  return 0;
}
