#include <bits/stdc++.h>
class Node {
 public:
  int id;
  int capacity;
  std::list<std::tuple<int, int, int, int>> edges;
  Node(int i) {
    id = i;
    capacity = 0;
  }
  void addNeighbor(int node_index, int flow, int direction, int edge_index) {
    edges.push_back(std::make_tuple(node_index, flow, direction, edge_index));
    capacity += flow;
  }
};
int main() {
  int n;
  int m;
  std::cin >> n >> m;
  Node* nodes[n];
  int edges[m];
  for (int i = 0; i < n; i++) {
    nodes[i] = new Node(i);
  }
  for (int i = 0; i < m; i++) {
    edges[i] = 0;
    int u, v, c;
    std::cin >> u >> v >> c;
    u--;
    v--;
    nodes[u]->addNeighbor(v, c, 0, i);
    nodes[v]->addNeighbor(u, c, 1, i);
  }
  nodes[0]->capacity = 0;
  nodes[n - 1]->capacity *= 2;
  std::queue<int> qq;
  qq.push(0);
  while (!qq.empty()) {
    Node* current = nodes[qq.front()];
    qq.pop();
    std::list<std::tuple<int, int, int, int>>::iterator it;
    for (it = current->edges.begin(); it != current->edges.end(); it++) {
      Node* temp = nodes[std::get<0>(*it)];
      int flow = std::get<1>(*it);
      int direction = std::get<2>(*it);
      int edge_index = std::get<3>(*it);
      if (temp->capacity != 0) {
        edges[edge_index] = direction;
        temp->capacity -= flow * 2;
        if (temp->capacity == 0) {
          qq.push(temp->id);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    std::cout << edges[i] << std::endl;
  }
  for (int i = 0; i < n; i++) {
    delete nodes[i];
  }
}
