#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 9;
int n, m, f[MAX], ans[MAX];
vector<pair<pair<int, int>, pair<int, int> > > g[MAX];
queue<int> q;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  memset(ans, -1, sizeof ans);
  cin >> n >> m;
  for (int i = 0, v, u, w; i < m; i++)
    cin >> v >> u >> w, v--, u--, g[v].push_back({{u, w}, {i, 0}}),
        g[u].push_back({{v, w}, {i, 1}}), f[v] += w, f[u] += w;
  q.push(0);
  while (q.size()) {
    int v = q.front();
    q.pop();
    if (v != n - 1)
      for (auto u : g[v])
        if (ans[u.second.first] == -1) {
          f[u.first.first] -= 2 * u.first.second,
              ans[u.second.first] = u.second.second;
          if (f[u.first.first] == 0 && u.first.first != 0)
            q.push(u.first.first);
        }
  }
  for (int i = 0; i < m; i++) cout << ans[i] << '\n';
}
