#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int v, c, i, d;
  Edge(int v, int c, int i, int d) : v(v), c(c), i(i), d(d) {}
};
vector<Edge> graph[200003];
int visit[200003];
int flow[200003];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (int)m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    graph[a].push_back(Edge(b, c, i, 0));
    graph[b].push_back(Edge(a, c, i, 1));
    flow[a] += c, flow[b] += c;
  }
  for (int i = 0; i < (int)n; i++) flow[i] /= 2;
  queue<int> Q;
  memset(visit, -1, sizeof(visit));
  Q.push(0);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < (int)(int)graph[u].size(); i++) {
      int id = graph[u][i].i;
      if (visit[id] == -1) {
        visit[id] = graph[u][i].d;
        flow[graph[u][i].v] -= graph[u][i].c;
        if (graph[u][i].v != n - 1 && flow[graph[u][i].v] == 0)
          Q.push(graph[u][i].v);
      }
    }
  }
  for (int i = 0; i < (int)m; i++) cout << visit[i] << endl;
  return 0;
}
