#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200500;
int known_in[MAXN], known_out[MAXN], unknown[MAXN];
int n, m;
map<pair<int, int>, bool> was;
vector<pair<int, int> > edges[MAXN];
pair<int, int> all_edges[MAXN];
void relax_out(int v, queue<int> &q) {
  for (const auto e : edges[v]) {
    int dest = e.first;
    auto mark = make_pair(v, dest);
    auto mark_rev = make_pair(dest, v);
    if (was.count(mark) or was.count(mark_rev)) continue;
    was[mark] = 1;
    was[mark_rev] = 0;
    known_in[dest] += e.second;
    unknown[dest] -= e.second;
    if (abs(known_in[dest] - known_out[dest]) == unknown[dest]) q.push(dest);
  }
}
void relax_in(int v, queue<int> &q) {
  for (const auto e : edges[v]) {
    int dest = e.first;
    auto mark = make_pair(v, dest);
    auto mark_rev = make_pair(dest, v);
    if (was.count(mark) or was.count(mark_rev)) continue;
    was[mark] = 0;
    was[mark_rev] = 1;
    known_out[dest] += e.second;
    unknown[dest] -= e.second;
    if (abs(known_in[dest] - known_out[dest]) == unknown[dest]) q.push(dest);
  }
}
int main() {
  cin.sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int fr, to, flow;
    cin >> fr >> to >> flow;
    edges[fr].push_back(make_pair(to, flow));
    edges[to].push_back(make_pair(fr, flow));
    all_edges[i] = make_pair(fr, to);
    unknown[fr] += flow;
    unknown[to] += flow;
  }
  queue<int> order;
  relax_out(1, order);
  relax_in(n, order);
  while (!order.empty()) {
    int v = order.front();
    order.pop();
    if (known_in[v] - known_out[v] == unknown[v])
      relax_out(v, order);
    else
      relax_in(v, order);
  }
  for (int i = 0; i < m; i++) {
    assert(was.count(all_edges[i]));
    cout << 1 - was[all_edges[i]] << "\n";
  }
  return 0;
}
