#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<pair<int, int> > adj[N];
vector<int> querie[N], tipo[N];
int ans[N], sum[N], parcial[N];
bool vis[N];
int main() {
  int n, m, x, y, w;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d", &x, &y, &w);
    x--;
    y--;
    adj[x].push_back(pair<int, int>(y, w));
    tipo[x].push_back(0);
    querie[x].push_back(i);
    adj[y].push_back(pair<int, int>(x, w));
    tipo[y].push_back(1);
    querie[y].push_back(i);
    sum[x] += w;
    sum[y] += w;
  }
  queue<int> q;
  q.push(0);
  vis[0] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int j = 0; j < adj[u].size(); ++j) {
      pair<int, int> v = adj[u][j];
      if (vis[v.first]) continue;
      ans[querie[u][j]] = tipo[u][j];
      parcial[v.first] += v.second;
      if (v.first != n - 1 && parcial[v.first] * 2 == sum[v.first]) {
        q.push(v.first);
        vis[v.first] = 1;
      }
    }
  }
  for (int i = 0; i < m; ++i) printf("%d\n", ans[i]);
  return 0;
}
