#include <bits/stdc++.h>
using namespace std;
const int MOD7 = 1000000000 + 7;
const int MOD9 = 1000000000 + 9;
vector<pair<int, int> > g[5000];
int status[5000] = {};
int ans[5000] = {};
void dfs(int x) {
  status[x] = 1;
  for (auto e : g[x]) {
    if (status[e.first] == 1) {
      ans[e.second] = 2;
    } else {
      ans[e.second] = 1;
    }
    if (!status[e.first]) {
      dfs(e.first);
    }
  }
  status[x] = 2;
}
int main() {
  int n = ({
    int x;
    scanf("%d", &x);
    x;
  }),
      m = ({
        int x;
        scanf("%d", &x);
        x;
      });
  for (int i = (0); i < (m); i += (1)) {
    int x = ({
      int x;
      scanf("%d", &x);
      x;
    }),
        y = ({
          int x;
          scanf("%d", &x);
          x;
        });
    x--;
    y--;
    g[x].push_back({y, i});
  }
  for (int i = (0); i < (n); i += (1))
    if (!status[i]) dfs(i);
  cout << *max_element(ans, ans + m) << "\n";
  for (int i = (0); i < (m); i += (1)) cout << ans[i] << " ";
  puts("");
}
