#include <bits/stdc++.h>
using namespace std;
long long read() {
  char ch = getchar();
  long long x = 0;
  int op = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') op = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 1) + ch - '0';
  return x * op;
}
int n, m, a[1005][1005], x[200005], y[200005], vis[1005], ans[200005];
vector<int> e[1005];
vector<int> id[1005];
void dfs(int p, int node, int dir) {
  if (node == p) return;
  if (a[p][node] >= 2) return;
  if (a[p][node] == 1 && vis[node] == dir) return;
  if (a[p][node] == 0) vis[node] = dir;
  a[p][node]++;
  for (int i = 0; i < e[node].size(); i++) dfs(p, e[node][i], dir);
}
int main() {
  n = read();
  m = read();
  for (int i = (1); i <= (m); i++) {
    x[i] = read(), y[i] = read();
    e[x[i]].push_back(y[i]);
    id[x[i]].push_back(i);
  }
  for (int i = (1); i <= (n); i++) {
    memset(vis, -1, sizeof(vis));
    for (int j = (0); j <= ((int)e[i].size() - 1); j++)
      dfs(i, e[i][j], e[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < e[i].size(); j++) {
      bool f = a[i][e[i][j]] >= 2;
      bool g = a[e[i][j]][i] >= 1;
      ans[id[i][j]] = f != g;
    }
  }
  return 0;
}
