#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int maxn = 200000 + 10;
const int inf = 1000000000;
ifstream fin("input.txt");
ofstream fout("output.txt");
int n, m, d[maxn], ans[maxn];
bool mark[maxn];
vector<int> v[maxn], num[maxn], c[maxn];
pair<int, int> edge[maxn];
queue<int> q;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, z;
    cin >> a >> b >> z;
    v[a].push_back(b);
    v[b].push_back(a);
    num[a].push_back(i);
    num[b].push_back(i);
    c[a].push_back(z);
    c[b].push_back(z);
    edge[i] = pair<int, int>(a, b);
    d[a] += z;
    d[b] += z;
  }
  for (int i = 2; i < n; i++) d[i] /= 2;
  q.push(1);
  mark[1] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 0; i < v[now].size(); i++) {
      int next = v[now][i];
      if (mark[next]) continue;
      d[next] -= c[now][i];
      if (edge[num[now][i]].first == now)
        ans[num[now][i]] = 0;
      else
        ans[num[now][i]] = 1;
      if (d[next] == 0 && next != n) {
        q.push(next);
        mark[next] = 1;
      }
    }
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << endl;
  return 0;
}
