Problem: There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions.

Changes are coming to Berland — the state decided to privatize BerAvia, namely, to sell out all flights to t private companies. Each of these companies wants to get the maximal number of flights, so if the Berland flights are sold unevenly, Berland can be accused of partiality. Berland Government decided to sell the flights as evenly as possible between the t companies.

The unevenness of the distribution of flights between companies is calculated as follows. For each city i (both Berland and Beerland) we'll calculate the value of 

<image> where aij is the number of flights from city i, which belong to company j. The sum of wi for all cities in both countries is called the unevenness of the distribution. The distribution with the minimal unevenness is the most even one.

Help the Berland government come up with the most even distribution plan of selling flights.

Input

The first input line contains four integers n, m, k and t (1 ≤ n, m, t ≤ 200;1 ≤ k ≤ 5000), where n, m are the numbers of cities in Berland and Beerland, correspondingly, k is the number of flights between them, and t is the number of private companies. Next k lines describe the flights, one per line, as pairs of positive integers xi, yi (1 ≤ xi ≤ n;1 ≤ yi ≤ m), where xi and yi are the indexes of cities in Berland and Beerland, correspondingly, connected by the i-th flight. There is at most one flight between any pair of cities, each flight connects cities of different countries. The cities in Berland are indexed from 1 to n, and in Beerland — from 1 to m.

Output

Print the unevenness of the sought plan on the first line. On the second line print a sequence of k integers c1, c2, ..., ck (1 ≤ ci ≤ t), where ci is the index of the company that should buy the i-th flight. Assume that the flights are indexed from 1 to k in the order they appear in the input. If there are multiple solutions, print any of them.

Examples

Input

3 5 8 2
1 4
1 3
3 3
1 2
1 1
2 1
1 5
2 2


Output

4
2 1 2 1 2 1 2 2 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class T>
void checkmin(T &t, T x) {
  if (x < t) t = x;
}
template <class T>
void checkmax(T &t, T x) {
  if (x > t) t = x;
}
struct MinCostMaxFlow {
  struct edge {
    int x, y, c, w;
    edge *next, *op;
  } g[440000], *ls[405], *fa[405];
  int n, S, T, flow, cost, e;
  int d[405], inQ[405];
  void init(int _n, int _S, int _T) {
    n = _n;
    S = _S;
    T = _T;
    e = 0;
    for (int i = 0; i < n; i++) ls[i] = NULL;
  }
  void addedge(int x, int y, int c, int w = 0) {
    g[e].x = x;
    g[e].y = y;
    g[e].c = c;
    g[e].w = w;
    g[e].op = &g[e + 1];
    g[e].next = ls[x];
    ls[x] = &g[e++];
    g[e].x = y;
    g[e].y = x;
    g[e].c = 0;
    g[e].w = -w;
    g[e].op = &g[e - 1];
    g[e].next = ls[y];
    ls[y] = &g[e++];
  }
  bool spfa() {
    queue<int> q;
    for (int i = 0; i < n; i++) {
      d[i] = 0x7FFFFFFF;
      inQ[i] = 0;
    }
    q.push(S);
    d[S] = 0;
    inQ[S] = 1;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      inQ[x] = 0;
      for (edge *t = ls[x]; t; t = t->next) {
        if (t->c && d[t->x] + t->w < d[t->y]) {
          d[t->y] = d[t->x] + t->w;
          fa[t->y] = t;
          if (!inQ[t->y]) {
            inQ[t->y] = 1;
            q.push(t->y);
          }
        }
      }
    }
    return d[T] != 0x7FFFFFFF;
  }
  void aug() {
    int nf = 0x7FFFFFFF;
    for (int i = T; i != S; i = fa[i]->x) checkmin(nf, fa[i]->c);
    for (int i = T; i != S; i = fa[i]->x) {
      fa[i]->c -= nf;
      fa[i]->op->c += nf;
    }
    flow += nf;
    cost += nf * d[T];
  }
  void MCMF() {
    flow = cost = 0;
    while (spfa()) {
      aug();
    }
  }
} flow;
const int N = 405;
int n, m, k, t;
int deg[N], tag[N];
int color[N][N];
vector<pair<int, int> > edge;
int main() {
  memset(color, 0xFF, sizeof(color));
  scanf("%d%d%d%d", &n, &m, &k, &t);
  for (int x, y, i = 0; i < k; i++) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    edge.push_back(pair<int, int>(x, y));
    deg[x]++;
    deg[n + y]++;
    color[x][n + y] = -2;
  }
  int ans = 0;
  for (int i = 0; i < m + n; i++) {
    ans += !!(deg[i] % t);
  }
  printf("%d\n", ans);
  for (int _i = t; _i >= 1; _i--) {
    int S = n + m;
    int T = n + m + 1;
    flow.init(n + m + 2, n + m, n + m + 1);
    for (int j = 0; j < n; j++) {
      flow.addedge(S, j, deg[j] / _i);
      if (deg[j] % _i) flow.addedge(S, j, 1, 1);
    }
    for (int j = 0; j < m; j++) {
      flow.addedge(n + j, T, deg[n + j] / _i);
      if (deg[n + j] % _i) flow.addedge(n + j, T, 1, 1);
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < m; k++) {
        if (color[j][n + k] == -2) {
          flow.addedge(j, n + k, 1);
        }
      }
    }
    flow.MCMF();
    for (int i = 1; i < flow.e; i += 2) {
      if (flow.g[i].x < S && flow.g[i].y < S && flow.g[i].c) {
        color[flow.g[i].y][flow.g[i].x] = _i;
        deg[flow.g[i].y]--;
        deg[flow.g[i].x]--;
      }
    }
    for (int i = 0; i < flow.e; i += 2) {
      if ((flow.g[i].x >= S || flow.g[i].y >= S) && !flow.g[i].w) {
        assert(!flow.g[i].c);
      }
    }
  }
  for (__typeof((edge).begin()) it = (edge).begin(); it != (edge).end(); it++) {
    printf("%d ", color[it->first][it->second + n]);
  }
}