#include <bits/stdc++.h>
using namespace std;
struct Vertex {
  int number;
  int flow;
  int num_of_edge;
  Vertex(int _number, int _flow, int _num_of_edge)
      : number(_number), flow(_flow), num_of_edge(_num_of_edge) {}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector) {
  for (size_t i = 0; i < vector.size(); ++i) {
    os << vector[i] << " ";
  }
  os << "\n";
  return os;
}
template <typename T, typename S>
std::ostream& operator<<(std::ostream& os, const std::pair<T, S>& p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
std::ostream& operator<<(std::ostream& os, const Vertex& v) {
  os << "(" << v.number << ", " << v.flow << ", " << v.num_of_edge << ")";
  return os;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<Vertex> > v(n);
  vector<pair<pair<int, int>, int> > edges(m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    v[a - 1].push_back(Vertex(b - 1, c, i));
    v[b - 1].push_back(Vertex(a - 1, c, i));
    edges[i] = make_pair(make_pair(a - 1, b - 1), -1);
  }
  vector<int> f(n, 0);
  for (int i = 1; i < n - 1; ++i) {
    for (int j = 0; j < v[i].size(); ++j) {
      f[i] += v[i][j].flow;
    }
    f[i] /= 2;
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int num_of_b_vertex = q.front();
    q.pop();
    for (int i = 0; i < v[num_of_b_vertex].size(); ++i) {
      Vertex e_vertex = v[num_of_b_vertex][i];
      if (edges[e_vertex.num_of_edge].second == -1) {
        (edges[e_vertex.num_of_edge].first.first == num_of_b_vertex)
            ? edges[e_vertex.num_of_edge].second = 0
            : edges[e_vertex.num_of_edge].second = 1;
        f[e_vertex.number] -= e_vertex.flow;
        if (e_vertex.number != n - 1 && f[e_vertex.number] == 0) {
          q.push(e_vertex.number);
        }
      }
    }
  }
  for (int i = 0; i < edges.size(); ++i) {
    cout << edges[i].second << "\n";
  }
  return 0;
}
