#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
class Graph {
 public:
  class Node {
   public:
    bool isVisited;
    int id, totalSum, incomingSum, outcomingSum, visitTime;
    vector<pair<Node *, int> > v;
  };
  static int cntVisited;
  int cntNodes;
  Node nodes[MAX_N];
  void Init(int cntNodes) {
    this->cntNodes = cntNodes;
    for (int i = 1; i <= cntNodes; i++) {
      nodes[i].id = i;
      nodes[i].totalSum = 0;
      nodes[i].incomingSum = 0;
      nodes[i].outcomingSum = 0;
    }
  }
  void BFS(Node *stNode) {
    queue<Node *> q;
    q.push(stNode);
    stNode->isVisited = true;
    stNode->visitTime = cntVisited++;
    while (!q.empty()) {
      Node *nd = q.front();
      q.pop();
      for (auto x : nd->v) {
        if (x.first->isVisited) {
          continue;
        }
        x.first->incomingSum += x.second;
        x.first->visitTime = cntVisited++;
        if (x.first->id != cntNodes &&
            x.first->incomingSum == x.first->totalSum / 2) {
          x.first->isVisited = true;
          q.push(x.first);
        }
      }
    }
  }
};
int Graph::cntVisited = 0;
int x[MAX_N], y[MAX_N];
Graph graph;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  graph.Init(n);
  for (int i = 0; i < m; i++) {
    int c;
    cin >> x[i] >> y[i] >> c;
    graph.nodes[x[i]].totalSum += c;
    graph.nodes[y[i]].totalSum += c;
    graph.nodes[x[i]].v.push_back({&graph.nodes[y[i]], c});
    graph.nodes[y[i]].v.push_back({&graph.nodes[x[i]], c});
  }
  graph.BFS(&graph.nodes[1]);
  for (int i = 0; i < m; i++) {
    cout << (graph.nodes[x[i]].visitTime > graph.nodes[y[i]].visitTime) << '\n';
  }
  return 0;
}
