#include <bits/stdc++.h>
using namespace std;
struct Node;
struct Edge {
  Node* from;
  Node* to;
  int flow;
  bool dir = false;
  Node* other(Node* first) {
    if (first == from) return to;
    return from;
  }
  void setDir(Node* first) {
    if (first == to) dir = true;
  }
};
struct Node {
  long long totalCount;
  long long currFilled;
  vector<Edge*> edges;
  bool seen = false;
};
Node nodes[200005];
Edge edges[200005];
int main() {
  int nbNode, nbEdge;
  scanf("%d%d", &nbNode, &nbEdge);
  int from, to, flow;
  for (int i = 0; i < nbEdge; i++) {
    scanf("%d%d%d", &from, &to, &flow);
    edges[i].from = &nodes[from - 1];
    edges[i].to = &nodes[to - 1];
    edges[i].flow = flow;
    nodes[from - 1].edges.push_back(&edges[i]);
    nodes[from - 1].totalCount += flow;
    nodes[to - 1].edges.push_back(&edges[i]);
    nodes[to - 1].totalCount += flow;
  }
  queue<Node*> ready;
  ready.push(&nodes[0]);
  while (!ready.empty()) {
    Node* curr = ready.front();
    ready.pop();
    curr->seen = true;
    for (Edge* edge : curr->edges) {
      Node* other = edge->other(curr);
      if (other->seen) continue;
      edge->setDir(curr);
      other->currFilled += edge->flow;
      if (other->currFilled * 2 == other->totalCount &&
          other != &nodes[nbNode - 1]) {
        ready.push(other);
      }
    }
  }
  for (int i = 0; i < nbEdge; i++) {
    printf("%d\n", (int)edges[i].dir);
  }
  return 0;
}
