#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
vector<pair<int, int> > g[MAXN];
vector<int> id[MAXN];
vector<pair<int, int> > elist;
long long tot[MAXN];
set<pair<int, int> > dirs;
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    tot[u] += w;
    tot[v] += w;
    g[u].push_back(make_pair(v, w));
    g[v].push_back(make_pair(u, w));
    id[u].push_back(i);
    id[v].push_back(i);
    elist.push_back(make_pair(u, v));
  }
  for (int i = 0; i < n; i++) tot[i] /= 2;
  vector<int> q;
  q.push_back(0);
  for (int i = 0; i < q.size(); i++) {
    int v = q[i];
    for (int j = 0; j < g[v].size(); j++) {
      if (dirs.find(make_pair(v, g[v][j].first)) != dirs.end()) continue;
      if (dirs.find(make_pair(g[v][j].first, v)) != dirs.end()) continue;
      dirs.insert(make_pair(v, g[v][j].first));
      tot[g[v][j].first] -= g[v][j].second;
      if (g[v][j].first != n - 1 && tot[g[v][j].first] == 0)
        q.push_back(g[v][j].first);
    }
  }
  for (int i = 0; i < elist.size(); i++) {
    if (dirs.find(make_pair(elist[i].first, elist[i].second)) != dirs.end())
      cout << "0\n";
    else
      cout << "1\n";
  }
  return 0;
}
