#include <bits/stdc++.h>
using namespace std;
class Edge {
 public:
  int to, cost, rev, index;
  Edge(int to0, int cost0, int rev0, int index0) {
    to = to0;
    cost = cost0;
    rev = rev0;
    index = index0;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<Edge> > edges(n + 1);
  vector<int> sum(n + 1, 0);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    edges[a].push_back(Edge(b, c, edges[b].size(), i));
    edges[b].push_back(Edge(a, c, edges[a].size() - 1, i + m));
    sum[a] += c;
    sum[b] += c;
  }
  multimap<int, int> mm;
  mm.insert(make_pair(0, 1));
  vector<int> ret(m);
  while (!mm.empty()) {
    int curr = mm.begin()->second;
    int tmp = mm.begin()->first;
    mm.erase(mm.begin());
    if (curr == n || tmp > sum[curr]) continue;
    for (unsigned i = 0; i < edges[curr].size(); ++i) {
      int next = edges[curr][i].to;
      if (next == -1) continue;
      sum[next] -= edges[curr][i].cost * 2;
      mm.insert(make_pair(sum[next], next));
      edges[next][edges[curr][i].rev].to = -1;
      ret[edges[curr][i].index % m] = edges[curr][i].index / m;
    }
  }
  for (int i = 0; i < m; ++i) cout << ret[i] << endl;
  return 0;
}
