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 JAVA):

Solution:
import java.util.*;

// BiGraph Matching

public class CF_212A {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
        while (task()) {
        }
    }

    private static int n, m, k, t;


    private static int maxK = 5000;
    private static int maxT = 2000;
    private static int[] a = new int[maxK + 10];
    private static int[] b = new int[maxK + 10];

    private static int[] X = new int[maxT * maxT + 10];
    private static int[] Y = new int[maxT * maxT + 10];
    private static int[] xBegin = new int[maxT * maxT + 10];
    private static int[] yBegin = new int[maxT * maxT + 10];

    private static int[] degree = new int[maxK + 10];
    private static int maxDegree = 0;

    private static int[] degreeX = new int[maxK + 10];
    private static int[] degreeY = new int[maxK + 10];

    private static short [][] col = new short[2 * maxT + 10][maxT + 10];
    private static short[][] colH = new short[2 * maxT + 10][2 * maxT + 10];

    private static boolean[][] realEdge = new boolean[2 * maxT + 10][2 * maxT + 10];
    // private static int[][] fakeEdge = new int[2 * maxT + 10][2 * maxT + 10];

    public static boolean task() {
        n = scanner.nextInt();
        m = scanner.nextInt();
        k = scanner.nextInt();
        t = scanner.nextInt();

        for (int i = 0; i < k; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            y += n;
            a[i] = x;
            b[i] = y;
            degree[x]++;
            degree[y]++;
        }

        int ans = 0;
        for (int i = 1; i <= n + m; i++) {
            ans += degree[i] % t == 0? 0: 1;
            // System.out.println("deg " + i + " " + degree[i]);
        }
        System.out.println(ans);

        if (t == 1) {
            for (int i = 0; i < k; i++) {
                if (i != 0) {
                    System.out.print(" ");
                }
                System.out.print("1");
            }
            System.out.println("");
            return false;
        }

        maxDegree = t;

        int hX = 1, hY = 1;
        for (int i = 1; i <= n; i++) {
            // System.out.println("deg " + i + " " + degree[i] + " " + hX);
            int deg = degree[i];
            xBegin[i] = hX;
            while (deg > 0) {
                X[hX] = i;
                hX++;
                deg -= maxDegree;
            }
        }

        for (int i = 1; i <= m; i++) {
            int deg = degree[n + i];
            yBegin[i] = hY;
            while (deg > 0) {
                Y[hY] = i;
                hY++;
                deg -= maxDegree;
            }
        }

        int h = hX - 1;
        if (h < hY - 1) {
            h = hY - 1;
        }

        /*
        for (int i = 1; i <= h; i++) {
            System.out.println(i + " " + X[i]);
        }

        System.out.println(" -- ");

        for (int i = 1; i <= h; i++) {
            System.out.println(i + " " + Y[i]);
        }
         */

        Matcher matcher = new Matcher(h, h);

        for (int i = 0; i < k; i++) {
            int x = a[i];
            int y = b[i] - n;
            int realX = xBegin[x];
            int realY = yBegin[y];
            matcher.addEdge(realX, realY);
            realEdge[realX][realY] = true;
            degreeX[realX]++;
            if (degreeX[realX] == maxDegree) {
                xBegin[x]++;
            }
            degreeY[realY]++;
            if (degreeY[realY] == maxDegree) {
                yBegin[y]++;
            }
        }

        int hy = 1;

        for (int i = 1; i <= h; i++) {
            int need = maxDegree - degreeX[i];
            while (need > 0) {
                while (degreeY[hy] == maxDegree) {
                    hy++;
                }
                matcher.addEdge(i, hy);
                // fakeEdge[i][hy] = 1;
                need--;
                degreeY[hy]++;
                degreeX[i]++;
            }
        }

        /*
        for (int i = 1; i <= h; i++) {
            System.out.println("check deg x " + i + " " + degreeX[i]);
        }
        for (int i = 1; i <= h; i++) {
            System.out.println("check deg y " + i + " " + degreeY[i]);
        }
         */

        for (int i = 0; i < maxDegree; i++) {
            matcher.match();
            // System.out.println(matcher.ans());
            int c = i + 1;
            for (int x = 1; x <= h; x++) {
                int y = matcher.matching[x] - h;
                if (y + h == -1) {
                    continue;
                }
                int originalX = X[x];
                int originalY = Y[y];
                if (originalX == 5 && originalY == 4) {
                    //System.out.println("hit !! " + originalX + " " + originalY + " " + col[originalX][originalY]);
                    //System.out.println("hit !! " + x + " " + y + " " + c);
                }
                if (realEdge[x][y] && colH[x][y] == 0) {
                    colH[x][y] = (short)c;
                    if (col[originalX][originalY] == 0) {
                        col[originalX][originalY] = (short)c;
                        //col[originalY][originalX] = c;
                    } else {
                    }
                }
               //  System.out.println("col " + originalX + " " + originalY + " " + c);
                matcher.delEdge(x, y);
            }
        }

        int tot = 0;
        for (int i = 0; i < k; i++) {
            int x = a[i];
            int y = b[i] - n;
            if (i > 0) {
                System.out.print(" ");
            }
            System.out.print(col[x][y]);
            tot++;
        }
        System.out.println();

        /*
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                System.out.print(col[i][j] + " ");
                if (col[i][j] > 0) {
                }
            }
            System.out.println();
        }
        System.out.println(tot);

        // check
        int [][] sumX = new int[maxT + 10][4];
        int [][] sumY = new int[maxT + 10][4];
        for (int i = 1; i <= n; i++) {
            sumX[i][1] = sumX[i][2] = sumX[i][3] = 0;
            sumY[i][1] = sumY[i][2] = sumY[i][3] = 0;
        }
        for (int i = 0; i < k; i++) {
            int x = a[i];
            int y = b[i] - n;
            sumX[x][col[x][y]]++;
            sumY[y][col[x][y]]++;
            if (x == 5) {
                System.out.println("check " + x  + " " + y + "  " + col[x][y]);
            }
        }
        for (int i = 1; i <= n; i++) {
            int min = sumX[i][1];
            int max = sumX[i][1];
            if (min > sumX[i][2]) {
                min = sumX[i][2];
            }
            if (min > sumX[i][3]) {
                min = sumX[i][3];
            }
            if (max < sumX[i][2]) {
                max = sumX[i][2];
            }
            if (max < sumX[i][3]) {
                max = sumX[i][3];
            }
            int yes = 0;
            if (degree[i] % 3 == 0) {
                if (min == max) {
                    yes = 1;
                }
            } else {
                if (min == max - 1) {
                    yes = 1;
                }
            }
            if (i == 5) {
                System.out.println("x " + i + " " + degree[i] + " " + min + " " + max + " " + yes);
            }
        }

        System.out.println("---");
        for (int i = 1; i <= m; i++) {
            int min = sumY[i][1];
            int max = sumY[i][1];
            if (min > sumY[i][2]) {
                min = sumY[i][2];
            }
            if (min > sumY[i][3]) {
                min = sumY[i][3];
            }
            if (max < sumY[i][2]) {
                max = sumY[i][2];
            }
            if (max < sumY[i][3]) {
                max = sumY[i][3];
            }
            int yes = 0;
            if (degree[i + n] % 3 == 0) {
                if (min == max) {
                    yes = 1;
                }
            } else {
                if (min == max - 1) {
                    yes = 1;
                }
            }
            System.out.println("y " + i + " " + degree[i + n] + " " + min + " " + max + " " + yes);
        }
         */



        return false;
    }



    public static final int maxN = 10000;

    public static class Matcher {
        public Map<Integer, Integer>[] g = new Map[2 * maxN + 10];
        private int nx, ny;
        private int nEdge;

        public static int[] matching = new int[2 * maxN + 10];
        private static int[] checked = new int[2 * maxN + 10];

        public Matcher(int nx, int ny) {
            this.nx = nx;
            this.ny = ny;
            nEdge = 0;
            for (int i = 0; i < g.length; i++) {
                g[i] = new HashMap<>();
            }
        }

        public void addEdge(int x, int y) {
            int now = g[x].getOrDefault(nx + y, 0);
            g[x].put(nx + y, now + 1);
            nEdge++;
            // System.out.println(("add " + x + " " + y));
        }

        public void delEdge(int x, int y) {
            int now = g[x].getOrDefault(nx + y, 0);
            if (now > 1) {
                g[x].put(nx + y, now - 1);
            } else {
                g[x].remove(nx + y);
            }
            nEdge--;
  //          System.out.println(("del " + x + " " + y));
        }

        public void reset() {
            ans = 0;
            for (int i = 0; i < matching.length; i++) {
                matching[i] = -1;
            }
            for (int i = 0; i < checked.length; i++) {
                checked[i] = -1;
            }
        }

        private int ans = 0;

        private Queue<Integer> q = new LinkedList<>();
        private int[] prev = new int[2 * maxN + 10];

        public void match() {
            reset();
            for (int x = 1; x <= nx; x++) {
                int step = x;
                if (matching[x] == -1) {
                    q.clear();
                    q.add(x);
                    prev[x] = -1;
                    boolean flag = false;
                    while (!q.isEmpty() && !flag) {
                        int u = q.poll();
                        //System.out.println(u);
                        for (int v: g[u].keySet()) {
                            if (flag) {
                                break;
                            }
                            if (checked[v] != step) {
                                checked[v] = step;
                                q.add(matching[v]);
                                if (matching[v] >= 0) {
                                    prev[matching[v]] = u;
                                } else {
                                    flag = true;
                                    int d = u;
                                    int e = v;
                                    while (d != -1) {
                                        int t = matching[d];
                                        matching[d] = e;
                                        matching[e] = d;
                                        d = prev[d];
                                        e = t;
                                    }
                                }
                            }
                        }
                        // q.poll();
                    }
                    if (matching[x] != -1) {
                        ans++;
                    }
                }
            }
        }

        public int ans() {
            return ans;
        }

    }

}