#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int u, v, cap;
  Edge(int u, int v, int cap) : u(u), v(v), cap(cap){};
};
int n, m, a, b, c, flow[200005], in_flow[200005], ans[200005];
vector<vector<int>> adjList;
vector<Edge> edgeList;
bitset<200005> visited;
void addEdge(int u, int v, int cap) {
  adjList[v].push_back(edgeList.size());
  adjList[u].push_back(edgeList.size());
  edgeList.push_back(Edge(u, v, cap));
  flow[u] += c;
  flow[v] += c;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  adjList.resize(n + 5);
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    addEdge(a, b, c);
  }
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < adjList[u].size(); i++) {
      int id = adjList[u][i];
      if (!visited[id]) {
        visited[id] = 1;
        Edge e = edgeList[id];
        if (e.u != u) {
          swap(e.u, e.v);
          ans[id] = 1;
        }
        in_flow[e.v] += e.cap;
        if (e.v != n && in_flow[e.v] == flow[e.v] / 2) {
          q.push(e.v);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
