#include <bits/stdc++.h>
struct edge {
  int idx;
  int weight;
  int direction;
  int start;
  int to;
};
std::vector<int> bfs(std::vector<std::vector<edge>>& edges,
                     std::vector<int>& sums, std::vector<int>& dirs) {
  std::vector<bool> visited(edges.size(), false);
  visited[0] = true;
  std::vector<int> q(1, 0);
  int q_index = 0;
  while (q_index < q.size()) {
    int node = q[q_index];
    for (auto e : edges[node]) {
      if (e.direction == -1) continue;
      if (visited[e.to]) continue;
      dirs[e.idx] = e.direction;
      sums[e.to] -= 2 * e.weight;
      if (sums[e.to] == 0 && e.to != sums.size() - 1) {
        q.push_back(e.to);
        visited[e.to] = true;
      }
    }
    q_index++;
  }
  return dirs;
}
int main() {
  int n_vertices, n_edges;
  std::cin >> n_vertices >> n_edges;
  std::vector<std::vector<edge>> edges(n_vertices, std::vector<edge>(0));
  std::vector<int> vec_sum(n_vertices, 0);
  std::vector<int> dirs(n_edges, -1);
  int i = 0;
  while (i < n_edges) {
    int s, t, w;
    std::cin >> s >> t >> w;
    s--;
    t--;
    edges[s].push_back(edge{i, w, 0, s, t});
    edges[t].push_back(edge{i, w, 1, t, s});
    vec_sum[s] += w;
    vec_sum[t] += w;
    i++;
  }
  auto directions = bfs(edges, vec_sum, dirs);
  for (auto d : directions) std::cout << d << std::endl;
  return EXIT_SUCCESS;
}
