import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in Actual solution is at the top
 */
public class A {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] b = new int[n];
            int[] g = new int[m];

            for (int i = 0; i < n; i++)
                b[i] = in.nextInt();
            for (int i = 0; i < m; i++)
                g[i] = in.nextInt();

            Arrays.sort(b);
            Arrays.sort(g);
            if (b[n-1] > g[0]) {
                out.println(-1);
                return ;
            }
            long res = 0;
            int j = m-1;
            int last = n-1;
            for (int i = n-1; i >= 0; i --) {
                res = res + (long)b[i] * m;
                while (j >= 0 && g[j] == b[last]) {
                    j--; last --;
                }
                for (int k = 0; k < m-1 && j >= 0; k ++) {
                    res = res + g[j] - b[i];
                    j--;
                }
            }
            out.println(res);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String nextString() {
            return next();
        }
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Copyright © 2018 Darshan. All rights reserved.Darshandarji
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n, m;
            n = in.nextInt();
            m = in.nextInt();
            int[] a = new int[n];
            int[] b = new int[m];
            long sum = 0, sum1 = 0;
            int mx = Integer.MIN_VALUE;
            int mi = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
                mx = Math.max(a[i], mx);
            }
            for (int i = 0; i < m; i++) {
                b[i] = in.nextInt();
                sum1 += b[i];
                mi = Math.min(mi, b[i]);
            }
            Arrays.sort(a);
            Arrays.sort(b);
            if (mi < mx) {
                out.println(-1);
            } else {
                long res = (m - 1) * (sum - a[n - 1]);
                res += sum;
                res += sum1;
                res -= a[n - 1];
                res -= b[0];
                if (a[n - 1] < b[0]) {
                    res += a[n - 1];
                    res -= a[n - 2];
                    res += b[0];
                } else {
                    res += b[0];
                }

                out.println(res);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Copyright © 2018 Darshan. All rights reserved.Darshandarji
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n, m;
            n = in.nextInt();
            m = in.nextInt();
            int[] a = new int[n];
            int[] b = new int[m];
            int sum = 0, sum1 = 0;
            int mx = Integer.MIN_VALUE;
            int mi = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                sum += a[i];
                mx = Math.max(a[i], mx);
            }
            for (int i = 0; i < m; i++) {
                b[i] = in.nextInt();
                sum1 += b[i];
                mi = Math.min(mi, b[i]);
            }
            Arrays.sort(a);
            Arrays.sort(b);
            if (mi < mx) {
                out.println(-1);
            } else {
                int res = (m - 1) * (sum - a[n - 1]);
                res += sum;
                res += sum1;
                res -= a[n - 1];
                res -= b[0];
                if (a[n - 1] < b[0]) {
                    res += a[n - 1];
                    res -= a[n - 2];
                    res += b[0];
                } else {
                    res += b[0];
                }

                out.println(res);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Copyright © 2018 Darshan. All rights reserved.Darshandarji
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n, m;
            n = in.nextInt();
            m = in.nextInt();
            int[] a = new int[n];
            int[] b = new int[m];
            int mx = Integer.MIN_VALUE;
            int mi = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                mx = Math.max(a[i], mx);
            }
            for (int i = 0; i < m; i++) {
                b[i] = in.nextInt();
                mi = Math.min(mi, b[i]);
            }
            Arrays.sort(a);
            Arrays.sort(b);
            if (mi < mx) {
                out.println(-1);
            } else {
                int res = 0;
                for (int i = m - 1; i >= 1; i--) {
                    res += b[i];
                }
                res += a[n - 1];
                if (a[n - 1] != b[0]) {
                    res += b[0];

                    res += (m - 1) * a[n - 2];
                    for (int i = n - 3; i >= 0; i--) {
                        res += a[i] * m;
                    }
                } else {
                    res += (m) * a[n - 2];
                    for (int i = n - 3; i >= 0; i--) {
                        res += a[i] * m;
                    }
                }

                out.println(res);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


// practice with rainboy
import java.io.*;
import java.util.*;

public class CF1159C {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine());
		Integer[] aa = new Integer[n];
		for (int i = 0; i < n; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine());
		Integer[] bb = new Integer[m];
		for (int j = 0; j < m; j++)
			bb[j] = Integer.parseInt(st.nextToken());
		Arrays.sort(aa);
		Arrays.sort(bb);
		if (bb[0] < aa[n - 1]) {
			System.out.println(-1);
			return;
		}
		long ans = 0;
		for (int i = 0; i < n; i++)
			ans += aa[i];
		ans *= m;
		for (int j = m - 1; j > 0; j--)
			ans += bb[j] - aa[n - 1];
		if (bb[0] > aa[n - 1])
		ans += bb[0] - aa[n - 2];
		System.out.println(ans);
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Osmanys Alonso (osmanys@gmail.com)
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int m = in.readInt();
            int b[] = new int[n];
            int g[] = new int[m];
            long totalb = 0;
            for (int i = 0; i < n; i++) {
                b[i] = in.readInt();
                totalb += b[i];
            }
            long totalg = 0;
            for (int i = 0; i < m; i++) {
                g[i] = in.readInt();
                totalg += g[i];
            }
            ArrayUtils.sort(b);
            ArrayUtils.sort(g);
            if (b[n - 1] > g[0])
                out.printLine(-1);
            else if (b[n - 1] == g[0]) {
                out.printLine(totalg + m * (totalb - b[n - 1]));
            } else {
                out.printLine(totalg + b[n - 1] + 1L * (m - 1) * b[n - 2] + m * (totalb - b[n - 1] - b[n - 2]));
            }
        }

    }

    static abstract class IntAbstractStream implements IntStream {
        public String toString() {
            StringBuilder builder = new StringBuilder();
            boolean first = true;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                if (first) {
                    first = false;
                } else {
                    builder.append(' ');
                }
                builder.append(it.value());
            }
            return builder.toString();
        }

        public boolean equals(Object o) {
            if (!(o instanceof IntStream)) {
                return false;
            }
            IntStream c = (IntStream) o;
            IntIterator it = intIterator();
            IntIterator jt = c.intIterator();
            while (it.isValid() && jt.isValid()) {
                if (it.value() != jt.value()) {
                    return false;
                }
                it.advance();
                jt.advance();
            }
            return !it.isValid() && !jt.isValid();
        }

        public int hashCode() {
            int result = 0;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                result *= 31;
                result += it.value();
            }
            return result;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(long i) {
            writer.println(i);
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static interface IntIterator {
        public int value() throws NoSuchElementException;

        public boolean advance();

        public boolean isValid();

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class ArrayUtils {
        public static int[] sort(int[] array) {
            return sort(array, IntComparator.DEFAULT);
        }

        public static int[] sort(int[] array, IntComparator comparator) {
            return sort(array, 0, array.length, comparator);
        }

        public static int[] sort(int[] array, int from, int to, IntComparator comparator) {
            if (from == 0 && to == array.length) {
                new IntArray(array).sort(comparator);
            } else {
                new IntArray(array).subList(from, to).sort(comparator);
            }
            return array;
        }

    }

    static class Sorter {
        private static final int INSERTION_THRESHOLD = 16;

        private Sorter() {
        }

        public static void sort(IntList list, IntComparator comparator) {
            quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1,
                    comparator);
        }

        private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) {
            if (to - from < INSERTION_THRESHOLD) {
                insertionSort(list, from, to, comparator);
                return;
            }
            if (remaining == 0) {
                heapSort(list, from, to, comparator);
                return;
            }
            remaining--;
            int pivotIndex = (from + to) >> 1;
            int pivot = list.get(pivotIndex);
            list.swap(pivotIndex, to);
            int storeIndex = from;
            int equalIndex = to;
            for (int i = from; i < equalIndex; i++) {
                int value = comparator.compare(list.get(i), pivot);
                if (value < 0) {
                    list.swap(storeIndex++, i);
                } else if (value == 0) {
                    list.swap(--equalIndex, i--);
                }
            }
            quickSort(list, from, storeIndex - 1, remaining, comparator);
            for (int i = equalIndex; i <= to; i++) {
                list.swap(storeIndex++, i);
            }
            quickSort(list, storeIndex, to, remaining, comparator);
        }

        private static void heapSort(IntList list, int from, int to, IntComparator comparator) {
            for (int i = (to + from - 1) >> 1; i >= from; i--) {
                siftDown(list, i, to, comparator, from);
            }
            for (int i = to; i > from; i--) {
                list.swap(from, i);
                siftDown(list, from, i - 1, comparator, from);
            }
        }

        private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) {
            int value = list.get(start);
            while (true) {
                int child = ((start - delta) << 1) + 1 + delta;
                if (child > end) {
                    return;
                }
                int childValue = list.get(child);
                if (child + 1 <= end) {
                    int otherValue = list.get(child + 1);
                    if (comparator.compare(otherValue, childValue) > 0) {
                        child++;
                        childValue = otherValue;
                    }
                }
                if (comparator.compare(value, childValue) >= 0) {
                    return;
                }
                list.swap(start, child);
                start = child;
            }
        }

        private static void insertionSort(IntList list, int from, int to, IntComparator comparator) {
            for (int i = from + 1; i <= to; i++) {
                int value = list.get(i);
                for (int j = i - 1; j >= from; j--) {
                    if (comparator.compare(list.get(j), value) <= 0) {
                        break;
                    }
                    list.swap(j, j + 1);
                }
            }
        }

    }

    static class IntArrayList extends IntAbstractStream implements IntList {
        private int size;
        private int[] data;

        public IntArrayList() {
            this(3);
        }

        public IntArrayList(int capacity) {
            data = new int[capacity];
        }

        public IntArrayList(IntCollection c) {
            this(c.size());
            addAll(c);
        }

        public IntArrayList(IntStream c) {
            this();
            if (c instanceof IntCollection) {
                ensureCapacity(((IntCollection) c).size());
            }
            addAll(c);
        }

        public IntArrayList(IntArrayList c) {
            size = c.size();
            data = c.data.clone();
        }

        public IntArrayList(int[] arr) {
            size = arr.length;
            data = arr.clone();
        }

        public int size() {
            return size;
        }

        public int get(int at) {
            if (at >= size) {
                throw new IndexOutOfBoundsException("at = " + at + ", size = " + size);
            }
            return data[at];
        }

        private void ensureCapacity(int capacity) {
            if (data.length >= capacity) {
                return;
            }
            capacity = Math.max(2 * data.length, capacity);
            data = Arrays.copyOf(data, capacity);
        }

        public void addAt(int index, int value) {
            ensureCapacity(size + 1);
            if (index > size || index < 0) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            if (index != size) {
                System.arraycopy(data, index, data, index + 1, size - index);
            }
            data[index] = value;
            size++;
        }

        public void removeAt(int index) {
            if (index >= size || index < 0) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            if (index != size - 1) {
                System.arraycopy(data, index + 1, data, index, size - index - 1);
            }
            size--;
        }

        public void set(int index, int value) {
            if (index >= size) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            data[index] = value;
        }

    }

    static interface IntReversableCollection extends IntCollection {
    }

    static class IntArray extends IntAbstractStream implements IntList {
        private int[] data;

        public IntArray(int[] arr) {
            data = arr;
        }

        public int size() {
            return data.length;
        }

        public int get(int at) {
            return data[at];
        }

        public void addAt(int index, int value) {
            throw new UnsupportedOperationException();
        }

        public void removeAt(int index) {
            throw new UnsupportedOperationException();
        }

        public void set(int index, int value) {
            data[index] = value;
        }

    }

    static interface IntList extends IntReversableCollection {
        public abstract int get(int index);

        public abstract void set(int index, int value);

        public abstract void addAt(int index, int value);

        public abstract void removeAt(int index);

        default public void swap(int first, int second) {
            if (first == second) {
                return;
            }
            int temp = get(first);
            set(first, get(second));
            set(second, temp);
        }

        default public IntIterator intIterator() {
            return new IntIterator() {
                private int at;
                private boolean removed;

                public int value() {
                    if (removed) {
                        throw new IllegalStateException();
                    }
                    return get(at);
                }

                public boolean advance() {
                    at++;
                    removed = false;
                    return isValid();
                }

                public boolean isValid() {
                    return !removed && at < size();
                }

                public void remove() {
                    removeAt(at);
                    at--;
                    removed = true;
                }
            };
        }

        default public void add(int value) {
            addAt(size(), value);
        }

        default public IntList sort(IntComparator comparator) {
            Sorter.sort(this, comparator);
            return this;
        }

        default public IntList subList(final int from, final int to) {
            return new IntList() {
                private final int shift;
                private final int size;

                {
                    if (from < 0 || from > to || to > IntList.this.size()) {
                        throw new IndexOutOfBoundsException("from = " + from + ", to = " + to + ", size = " + size());
                    }
                    shift = from;
                    size = to - from;
                }

                public int size() {
                    return size;
                }

                public int get(int at) {
                    if (at < 0 || at >= size) {
                        throw new IndexOutOfBoundsException("at = " + at + ", size = " + size());
                    }
                    return IntList.this.get(at + shift);
                }

                public void addAt(int index, int value) {
                    throw new UnsupportedOperationException();
                }

                public void removeAt(int index) {
                    throw new UnsupportedOperationException();
                }

                public void set(int at, int value) {
                    if (at < 0 || at >= size) {
                        throw new IndexOutOfBoundsException("at = " + at + ", size = " + size());
                    }
                    IntList.this.set(at + shift, value);
                }

                public IntList compute() {
                    return new IntArrayList(this);
                }
            };
        }

    }

    static interface IntComparator {
        public static final IntComparator DEFAULT = (first, second) -> {
            if (first < second) {
                return -1;
            }
            if (first > second) {
                return 1;
            }
            return 0;
        };

        public int compare(int first, int second);

    }

    static interface IntCollection extends IntStream {
        public int size();

        default public void add(int value) {
            throw new UnsupportedOperationException();
        }

        default public IntCollection addAll(IntStream values) {
            for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) {
                add(it.value());
            }
            return this;
        }

    }

    static interface IntStream extends Iterable<Integer>, Comparable<IntStream> {
        public IntIterator intIterator();

        default public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                private IntIterator it = intIterator();

                public boolean hasNext() {
                    return it.isValid();
                }

                public Integer next() {
                    int result = it.value();
                    it.advance();
                    return result;
                }
            };
        }

        default public int compareTo(IntStream c) {
            IntIterator it = intIterator();
            IntIterator jt = c.intIterator();
            while (it.isValid() && jt.isValid()) {
                int i = it.value();
                int j = jt.value();
                if (i < j) {
                    return -1;
                } else if (i > j) {
                    return 1;
                }
                it.advance();
                jt.advance();
            }
            if (it.isValid()) {
                return 1;
            }
            if (jt.isValid()) {
                return -1;
            }
            return 0;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Osmanys Alonso (osmanys@gmail.com)
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int m = in.readInt();
            int b[] = new int[n];
            int g[] = new int[m];
            long totalb = 0;
            for (int i = 0; i < n; i++) {
                b[i] = in.readInt();
                totalb += b[i];
            }
            long totalg = 0;
            for (int i = 0; i < m; i++) {
                g[i] = in.readInt();
                totalg += g[i];
            }
            ArrayUtils.sort(b);
            ArrayUtils.sort(g);
            if (b[n - 1] > g[0])
                out.printLine(-1);
            else if (b[n - 1] == g[0]) {
                out.printLine(totalg + m * (totalb - b[n - 1]));
            } else {
                out.printLine(totalg + b[n - 1] + (m - 1) * b[n - 2] + m * (totalb - b[n - 1] - b[n - 2]));
            }
        }

    }

    static class Sorter {
        private static final int INSERTION_THRESHOLD = 16;

        private Sorter() {
        }

        public static void sort(IntList list, IntComparator comparator) {
            quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1,
                    comparator);
        }

        private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) {
            if (to - from < INSERTION_THRESHOLD) {
                insertionSort(list, from, to, comparator);
                return;
            }
            if (remaining == 0) {
                heapSort(list, from, to, comparator);
                return;
            }
            remaining--;
            int pivotIndex = (from + to) >> 1;
            int pivot = list.get(pivotIndex);
            list.swap(pivotIndex, to);
            int storeIndex = from;
            int equalIndex = to;
            for (int i = from; i < equalIndex; i++) {
                int value = comparator.compare(list.get(i), pivot);
                if (value < 0) {
                    list.swap(storeIndex++, i);
                } else if (value == 0) {
                    list.swap(--equalIndex, i--);
                }
            }
            quickSort(list, from, storeIndex - 1, remaining, comparator);
            for (int i = equalIndex; i <= to; i++) {
                list.swap(storeIndex++, i);
            }
            quickSort(list, storeIndex, to, remaining, comparator);
        }

        private static void heapSort(IntList list, int from, int to, IntComparator comparator) {
            for (int i = (to + from - 1) >> 1; i >= from; i--) {
                siftDown(list, i, to, comparator, from);
            }
            for (int i = to; i > from; i--) {
                list.swap(from, i);
                siftDown(list, from, i - 1, comparator, from);
            }
        }

        private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) {
            int value = list.get(start);
            while (true) {
                int child = ((start - delta) << 1) + 1 + delta;
                if (child > end) {
                    return;
                }
                int childValue = list.get(child);
                if (child + 1 <= end) {
                    int otherValue = list.get(child + 1);
                    if (comparator.compare(otherValue, childValue) > 0) {
                        child++;
                        childValue = otherValue;
                    }
                }
                if (comparator.compare(value, childValue) >= 0) {
                    return;
                }
                list.swap(start, child);
                start = child;
            }
        }

        private static void insertionSort(IntList list, int from, int to, IntComparator comparator) {
            for (int i = from + 1; i <= to; i++) {
                int value = list.get(i);
                for (int j = i - 1; j >= from; j--) {
                    if (comparator.compare(list.get(j), value) <= 0) {
                        break;
                    }
                    list.swap(j, j + 1);
                }
            }
        }

    }

    static abstract class IntAbstractStream implements IntStream {
        public String toString() {
            StringBuilder builder = new StringBuilder();
            boolean first = true;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                if (first) {
                    first = false;
                } else {
                    builder.append(' ');
                }
                builder.append(it.value());
            }
            return builder.toString();
        }

        public boolean equals(Object o) {
            if (!(o instanceof IntStream)) {
                return false;
            }
            IntStream c = (IntStream) o;
            IntIterator it = intIterator();
            IntIterator jt = c.intIterator();
            while (it.isValid() && jt.isValid()) {
                if (it.value() != jt.value()) {
                    return false;
                }
                it.advance();
                jt.advance();
            }
            return !it.isValid() && !jt.isValid();
        }

        public int hashCode() {
            int result = 0;
            for (IntIterator it = intIterator(); it.isValid(); it.advance()) {
                result *= 31;
                result += it.value();
            }
            return result;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(long i) {
            writer.println(i);
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static interface IntIterator {
        public int value() throws NoSuchElementException;

        public boolean advance();

        public boolean isValid();

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class ArrayUtils {
        public static int[] sort(int[] array) {
            return sort(array, IntComparator.DEFAULT);
        }

        public static int[] sort(int[] array, IntComparator comparator) {
            return sort(array, 0, array.length, comparator);
        }

        public static int[] sort(int[] array, int from, int to, IntComparator comparator) {
            if (from == 0 && to == array.length) {
                new IntArray(array).sort(comparator);
            } else {
                new IntArray(array).subList(from, to).sort(comparator);
            }
            return array;
        }

    }

    static interface IntReversableCollection extends IntCollection {
    }

    static class IntArray extends IntAbstractStream implements IntList {
        private int[] data;

        public IntArray(int[] arr) {
            data = arr;
        }

        public int size() {
            return data.length;
        }

        public int get(int at) {
            return data[at];
        }

        public void addAt(int index, int value) {
            throw new UnsupportedOperationException();
        }

        public void removeAt(int index) {
            throw new UnsupportedOperationException();
        }

        public void set(int index, int value) {
            data[index] = value;
        }

    }

    static interface IntList extends IntReversableCollection {
        public abstract int get(int index);

        public abstract void set(int index, int value);

        public abstract void addAt(int index, int value);

        public abstract void removeAt(int index);

        default public void swap(int first, int second) {
            if (first == second) {
                return;
            }
            int temp = get(first);
            set(first, get(second));
            set(second, temp);
        }

        default public IntIterator intIterator() {
            return new IntIterator() {
                private int at;
                private boolean removed;

                public int value() {
                    if (removed) {
                        throw new IllegalStateException();
                    }
                    return get(at);
                }

                public boolean advance() {
                    at++;
                    removed = false;
                    return isValid();
                }

                public boolean isValid() {
                    return !removed && at < size();
                }

                public void remove() {
                    removeAt(at);
                    at--;
                    removed = true;
                }
            };
        }

        default public void add(int value) {
            addAt(size(), value);
        }

        default public IntList sort(IntComparator comparator) {
            Sorter.sort(this, comparator);
            return this;
        }

        default public IntList subList(final int from, final int to) {
            return new IntList() {
                private final int shift;
                private final int size;

                {
                    if (from < 0 || from > to || to > IntList.this.size()) {
                        throw new IndexOutOfBoundsException("from = " + from + ", to = " + to + ", size = " + size());
                    }
                    shift = from;
                    size = to - from;
                }

                public int size() {
                    return size;
                }

                public int get(int at) {
                    if (at < 0 || at >= size) {
                        throw new IndexOutOfBoundsException("at = " + at + ", size = " + size());
                    }
                    return IntList.this.get(at + shift);
                }

                public void addAt(int index, int value) {
                    throw new UnsupportedOperationException();
                }

                public void removeAt(int index) {
                    throw new UnsupportedOperationException();
                }

                public void set(int at, int value) {
                    if (at < 0 || at >= size) {
                        throw new IndexOutOfBoundsException("at = " + at + ", size = " + size());
                    }
                    IntList.this.set(at + shift, value);
                }

                public IntList compute() {
                    return new IntArrayList(this);
                }
            };
        }

    }

    static interface IntComparator {
        public static final IntComparator DEFAULT = (first, second) -> {
            if (first < second) {
                return -1;
            }
            if (first > second) {
                return 1;
            }
            return 0;
        };

        public int compare(int first, int second);

    }

    static interface IntCollection extends IntStream {
        public int size();

        default public void add(int value) {
            throw new UnsupportedOperationException();
        }

        default public IntCollection addAll(IntStream values) {
            for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) {
                add(it.value());
            }
            return this;
        }

    }

    static class IntArrayList extends IntAbstractStream implements IntList {
        private int size;
        private int[] data;

        public IntArrayList() {
            this(3);
        }

        public IntArrayList(int capacity) {
            data = new int[capacity];
        }

        public IntArrayList(IntCollection c) {
            this(c.size());
            addAll(c);
        }

        public IntArrayList(IntStream c) {
            this();
            if (c instanceof IntCollection) {
                ensureCapacity(((IntCollection) c).size());
            }
            addAll(c);
        }

        public IntArrayList(IntArrayList c) {
            size = c.size();
            data = c.data.clone();
        }

        public IntArrayList(int[] arr) {
            size = arr.length;
            data = arr.clone();
        }

        public int size() {
            return size;
        }

        public int get(int at) {
            if (at >= size) {
                throw new IndexOutOfBoundsException("at = " + at + ", size = " + size);
            }
            return data[at];
        }

        private void ensureCapacity(int capacity) {
            if (data.length >= capacity) {
                return;
            }
            capacity = Math.max(2 * data.length, capacity);
            data = Arrays.copyOf(data, capacity);
        }

        public void addAt(int index, int value) {
            ensureCapacity(size + 1);
            if (index > size || index < 0) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            if (index != size) {
                System.arraycopy(data, index, data, index + 1, size - index);
            }
            data[index] = value;
            size++;
        }

        public void removeAt(int index) {
            if (index >= size || index < 0) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            if (index != size - 1) {
                System.arraycopy(data, index + 1, data, index, size - index - 1);
            }
            size--;
        }

        public void set(int index, int value) {
            if (index >= size) {
                throw new IndexOutOfBoundsException("at = " + index + ", size = " + size);
            }
            data[index] = value;
        }

    }

    static interface IntStream extends Iterable<Integer>, Comparable<IntStream> {
        public IntIterator intIterator();

        default public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                private IntIterator it = intIterator();

                public boolean hasNext() {
                    return it.isValid();
                }

                public Integer next() {
                    int result = it.value();
                    it.advance();
                    return result;
                }
            };
        }

        default public int compareTo(IntStream c) {
            IntIterator it = intIterator();
            IntIterator jt = c.intIterator();
            while (it.isValid() && jt.isValid()) {
                int i = it.value();
                int j = jt.value();
                if (i < j) {
                    return -1;
                } else if (i > j) {
                    return 1;
                }
                it.advance();
                jt.advance();
            }
            if (it.isValid()) {
                return 1;
            }
            if (jt.isValid()) {
                return -1;
            }
            return 0;
        }

    }
}


import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main1159C
{
    static int[] boy;
    static int[] girl;
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        boy = new int[N];
        girl = new int[M];
        st = new StringTokenizer(br.readLine());
        int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE;
        long sum = 0;
        for(int i=0;i<N;++i)
        {
            boy[i] = Integer.parseInt(st.nextToken());
            max = Math.max(max,boy[i]);
            sum += boy[i];
        }
        st = new StringTokenizer(br.readLine());
        long sum2 = 0;
        for(int i=0;i<M;++i)
        {
            girl[i] = Integer.parseInt(st.nextToken());
            min = Math.min(min,girl[i]);
            sum2+=girl[i];
        }
        sum2 -= min;
        if(max>min)
        {
            System.out.print(-1); return;
        }
        Arrays.sort(boy);
        int s_max = min==max?max:boy[boy.length-2];
        sum *= M;
        sum -= (M-1)*max;
        sum += sum2;
        sum -= s_max;
        sum += min;
        bw.write(sum+"");
        bw.close();
    }

}


import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main1159C
{
    static int[] boy;
    static int[] girl;
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        boy = new int[N];
        girl = new int[M];
        st = new StringTokenizer(br.readLine());
        int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE;
        long sum = 0;
        for(int i=0;i<N;++i)
        {
            boy[i] = Integer.parseInt(st.nextToken());
            max = Math.max(max,boy[i]);
            sum += boy[i];
        }
        st = new StringTokenizer(br.readLine());
        long sum2 = 0;
        for(int i=0;i<M;++i)
        {
            girl[i] = Integer.parseInt(st.nextToken());
            min = Math.min(min,girl[i]);
            sum2+=girl[i];
        }
        sum2 -= min;
        if(max>min)
        {
            System.out.print(-1); return;
        }
        Arrays.sort(boy);
        int s_max = min==max?max:boy[boy.length-2];
        sum *= M;
        sum -= (long)(M-1)*(long)max;
        sum += sum2;
        sum -= s_max;
        sum += min;
        bw.write(sum+"");
        bw.close();
    }

}


import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main1159C
{
    static int[] boy;
    static int[] girl;
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        boy = new int[N];
        girl = new int[M];
        st = new StringTokenizer(br.readLine());
        int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE;
        long sum = 0;
        for(int i=0;i<N;++i)
        {
            boy[i] = Integer.parseInt(st.nextToken());
            max = Math.max(max,boy[i]);
            sum += boy[i];
        }
        if(max==0)
        {
            System.out.print(-1);
            return;
        }
        st = new StringTokenizer(br.readLine());
        long sum2 = 0;
        for(int i=0;i<M;++i)
        {
            girl[i] = Integer.parseInt(st.nextToken());
            min = Math.min(min,girl[i]);
            sum2+=girl[i];
        }
        sum2 -= min;
        if(max>min)
        {
            System.out.print(-1); return;
        }
        Arrays.sort(boy);
        int s_max = boy[boy.length-2] == 0? boy[boy.length-1]:boy[boy.length-2];
        sum *= M;
        sum -= (M-1)*max;
        sum += sum2;
        sum -= s_max;
        sum += min;
        bw.write(sum+"");
        bw.close();
    }

}


import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main1159C
{
    static int[] boy;
    static int[] girl;
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        boy = new int[N];
        girl = new int[M];
        st = new StringTokenizer(br.readLine());
        int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE;
        long sum = 0;
        for(int i=0;i<N;++i)
        {
            boy[i] = Integer.parseInt(st.nextToken());
            max = Math.max(max,boy[i]);
            sum += boy[i];
        }
        st = new StringTokenizer(br.readLine());
        long sum2 = 0;
        for(int i=0;i<M;++i)
        {
            girl[i] = Integer.parseInt(st.nextToken());
            min = Math.min(min,girl[i]);
            sum2+=girl[i];
        }
        sum2 -= min;
        if(max>min)
        {
            System.out.print(-1); return;
        }
        Arrays.sort(boy);
        int s_max = boy[boy.length-2] == 0? boy[boy.length-1]:boy[boy.length-2];
        sum *= M;
        sum -= (M-1)*max;
        sum += sum2;
        sum -= s_max;
        sum += min;
        bw.write(sum+"");
        bw.close();
    }

}


import java.util.Scanner;

public class C559 {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int m = input.nextInt();
		int maxb = 0;
		int b2 = 0;
		int ming = Integer.MAX_VALUE;
		long sumb = 0;
		long sumg = 0;
		for (int i = 0; i < n; i++){
			int b = input.nextInt();
			sumb += b;
			if (b > maxb){
				b2 = maxb;
				maxb = b;
			}else{
				b2 = Math.max(b2, b);
			}
		}
		for (int i = 0; i < m; i++){
			int g = input.nextInt();
			sumg += g;
			ming = Math.min(ming, g);
		}
		if (maxb > ming){
			System.out.println(-1);
		}else if (maxb == ming){
			System.out.println(sumg + m * (sumb - maxb));
		}else{
			System.out.println(sumg + m * (sumb - maxb) + maxb - b2);
		}
	}

}


#include <bits/stdc++.h>

using namespace std;

const int M=5050;
bool  g[M][M];
bool  visit[M];
bool  flag;
int   v,e;
int ans[M];
bool dfs(int i,int pre)
{
    visit[i]=true;
    for(int j=1;j<=v;j++)
        if(g[i][j])
        {
            if(!visit[j])
                return dfs(j,i);
            else if(j!=pre)  //如果访问过，且不是其父节点，那么就构成环
                return false;
        }
        return true;
}

int main()
{
    int i,j;
    while(cin>>v>>e)
    {
       // memset(g,0,sizeof(g));
        for(int i=1;i<=e;++i)
        {
        	int u,v;
            cin>>u>>v;
            g[u][v]=true;
            if(u<v)ans[i]=1;
            else ans[i]=2;
        }
        //memset(visit,0,sizeof(visit));
        flag=dfs(1,0);

        if(!flag){
        	cout<<2<<endl;
        	for(int i=1;i<=e;i++)
        	cout<<ans[i]<<" ";
        }

        else{
			cout<<1<<endl;
			for(int i=1;i<=e;i++)
			cout<<1<<" ";

		}
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
/*-------------------------------------------------------------------*/
typedef long long ll;
const int maxn=5010;
int du[maxn];
ll cnt;
int n,m;
int s;
vector<int>G[maxn];
pair<int,int>ans[maxn];
queue<int>q;
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    //一个环上的序号必然是非全递增的
	cin>>n>>m;
	for(int i=1;i<=m;i++){

		int u,v;
		cin>>u>>v;

		G[u].push_back(v);

		ans[i].first=u;ans[i].second=v;

		//入度
		du[v]++;
	}

	for(int i=1;i<=n;i++) if(du[i]==0) q.push(i);

	while(!q.empty()){

		int now=q.front();q.pop();

		int len=G[now].size();

		for(int i=0;i<len;i++){

			du[G[now][i]]--;//该点入度-1

			if(du[G[now][i]]==0)q.push(G[now][i]);

		}
	}
    int flag=0;

    for(int i=1;i<=n;i++)
	if(du[i]!=0){

		flag=1;//标记是否还存在入度不为0的点
		break;
	}
	//一个环上的序号必然是非全递增的

	if(flag){//说明有环
		cout<<2<<endl;
    	//for(int i=1;i<=m;i++)cout<<1<<" ";
    	for(int i=1;i<=m;i++){
    		if(ans[i].first>ans[i].second)
    		cout<<2<<" ";
    		else cout<<1<<" ";
    	}
	}
    else{ //无环直接输出1

    	cout<<1<<endl;
		for(int i=1;i<=m;i++)cout<<1<<" ";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int M=5050;
bool  g[M][M];
bool  visit[M];
bool  flag;
int   v,e;
int ans[M];
bool dfs(int i,int pre)
{
    visit[i]=true;
    for(int j=1;j<=v;j++)
        if(g[i][j])
        {
            if(!visit[j])
                return dfs(j,i);
            else if(j!=pre)  //如果访问过，且不是其父节点，那么就构成环
                return false;
        }
        return true;
}

int main()
{
    int i,j;
    while(cin>>v>>e)
    {
       // memset(g,0,sizeof(g));
        for(int i=1;i<=e;++i)
        {
        	int a,b;
            cin>>a>>b;
            g[a][b]=true;
            if(a<b)ans[i]=1;
            else ans[i]=2;
        }
        //memset(visit,0,sizeof(visit));
        flag=dfs(1,0);

        if(!flag){
        	cout<<2<<endl;
        	for(int i=1;i<=e;i++)
        	cout<<ans[i]<<" ";
        }

        else{
			cout<<1<<endl;
			for(int i=1;i<=e;i++)
			cout<<1<<" ";

		}
    }
    return 0;
}


//#include <bits/stdc++.h>
//#include <time.h>
//#define fi first
//#define se second
//#define endll "\n"
//#define MS0(X) memset((X), 0, sizeof((X)))
//#define MS1(X) memset((X), -1, sizeof((X)))
//#define LEN(X) strlen(X)
//#define rg(a,x,y) (a+x), (a+y+1)
//#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
//#define all(x) (x).begin(),(x).end()
//#define rall(x) (x).rbegin(),(x).rend()
//#define _sum(a) accumulate(a, 0ll)
//#define _max(a,b) *max_element(a,b)
//#define pb push_back
//#define pr make_pair
//#define bugc(_) cerr << (#_) << " = " << (_) << endl;
//#define sz(x) ((int)x.size())
//#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
//#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
//#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
//#define ios  ios::sync_with_stdio(false);
//#define lson l,mid,rt << 1
//#define rson mid + 1,r,rt << 1 | 1
/////vector(len,val);
//using namespace std;
//
//typedef long long ll;
//typedef double db;
//int xx[4] = {1,-1,0,0};
//int yy[4] = {0,0,1,-1};
//const double eps = 1e-9;
//typedef pair<int,int>  P;
//typedef pair<ll,ll>  Pl;
//typedef pair<double,double>  PP;
//const int maxn = 2e6 + 5000;
//const ll mod = 1e9 + 7;
//const int INF(0x3f3f3f3f);
//const ll INFL(0x3f3f3f3f3f3f3f3fll);
//inline int sign(db a) { return a < -eps ? -1 : a > eps;}
////inline int cmp(db a,db b){ return sign(a - b);}
//
//ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
//ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
//ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
//template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
//template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
//template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
//template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
//template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
//template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
//template <typename A> inline void debug(A a){cout << a << '\n';}
//template <typename A> inline ll sqr(A x){return 1ll * x * x;}
//template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
//template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
//template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
//template<class T>void wt(const T& x){cout << x << endl;}
//template<class T>void wt(const T& x, char c){cout << x << c;}
//template<class T>void wt(const T& x, const string& s){cout << x << s;}
//template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
//template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
//inline ll lowbit(ll x){ return (x & (-x)); }
//inline void debug(){  cout << "###!!!" << endl;}
////inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
////inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
// ll n;
//Pl p[maxn];
//bool cmp(P a,P b) {
//    return a.fi - a.se >= b.fi - b.se;
//}
//ll op[33];
//ll l[maxn];
//int main() {
//    int t;
//    op[1] = 1;
//    for(int i = 2;i <= 20;i++) op[i] = op[i - 1] * 2;
//    cin >> t;
//    while(t--) {
//        string str;
//        cin >> str; ll ans = 0; int id = 0;
//        for(int i = 0;i < str.size();i++){
//                l[i] = 0;   if(str[i] == '1') l[i] = id,id = i;
//        }
//        for(int i = str.size() - 1;i >= 0;i--){
//            ll val = 0,cnt = 1;
//            if(str[i] == '1') val += 1,ans++;
//            for(int j = i - 1,k = 0;j >= 0 && k <= 20;k++,j--){
//                cnt++;
//                if(str[j] == '1') val += op[cnt];
//                if(val > cnt){
//                    int cha = val - cnt;
//                    if(l[j] - j - 1 >= cha)ans++;
//                }
//                if(val == cnt) ans++;
//            }
//            bugc(ans);
//        } wt(ans);
//    }
//    return 0;
//}
//
//

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define rg(a,x,y) (a+x), (a+y+1)
#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define _sum(a) accumulate(a, 0ll)
#define _max(a,b) *max_element(a,b)
#define pb push_back
#define pr make_pair
#define bugc(_) cerr << (#_) << " = " << (_) << endl;
#define sz(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
#define ios  ios::sync_with_stdio(false);
#define lson l,mid,rt << 1
#define rson mid + 1,r,rt << 1 | 1
///vector(len,val);
using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
typedef pair<ll,ll>  Pl;
typedef pair<double,double>  PP;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
const int INF(0x3f3f3f3f);
const ll INFL(0x3f3f3f3f3f3f3f3fll);
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
//inline int cmp(db a,db b){ return sign(a - b);}

ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline ll sqr(A x){return 1ll * x * x;}
template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template<class T>void wt(const T& x){cout << x << endl;}
template<class T>void wt(const T& x, char c){cout << x << c;}
template<class T>void wt(const T& x, const string& s){cout << x << s;}
template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
inline ll lowbit(ll x){ return (x & (-x)); }
inline void debug(){  cout << "###!!!" << endl;}
//inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
//inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
 ll n;
Pl p[maxn];
bool cmp(P a,P b) {
    return a.fi - a.se >= b.fi - b.se;
}
ll op[33];
ll l[maxn];
int vis[5050][5050];
int main() {
    int m;
    while(cin >> n >> m){
        vector<int>v;
        for(int i = 1;i <= m;i++){int x,y;
    cin >>x >>y;
            if(x < y) v.pb(1);
            else v.pb(2);
        }
        int ans = 1;
        for(auto d:v) if(d == 2) ans = 2;
        wt(ans);
        for(auto d:v) cout << d << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define rg(a,x,y) (a+x), (a+y+1)
#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define _sum(a) accumulate(a, 0ll)
#define _max(a,b) *max_element(a,b)
#define pb push_back
#define pr make_pair
#define bugc(_) cerr << (#_) << " = " << (_) << endl;
#define sz(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
#define ios  ios::sync_with_stdio(false);
#define lson l,mid,rt << 1
#define rson mid + 1,r,rt << 1 | 1
///vector(len,val);
using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
typedef pair<ll,ll>  Pl;
typedef pair<double,double>  PP;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
const int INF(0x3f3f3f3f);
const ll INFL(0x3f3f3f3f3f3f3f3fll);
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
//inline int cmp(db a,db b){ return sign(a - b);}

ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline ll sqr(A x){return 1ll * x * x;}
template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template<class T>void wt(const T& x){cout << x << endl;}
template<class T>void wt(const T& x, char c){cout << x << c;}
template<class T>void wt(const T& x, const string& s){cout << x << s;}
template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
inline ll lowbit(ll x){ return (x & (-x)); }
inline void debug(){  cout << "###!!!" << endl;}
//inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
//inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
 ll n;
 int vis[5050];
 vector<int>v[maxn];
 void dfs(int x){
    vis[x] = 1;
    for(auto &d:v[x]) if(!vis[d]) dfs(d);
    return ;
 }
 bool check(int x,int y){
    MS0(vis);
     dfs(x);
     return vis[y];
 }
int main() {
    int m;ios;
    while(cin >> n >> m) {
        vector<int>vv;
        int flag = 0;
        for(int i = 1; i <= m; i++) {
            int x,y;
            cin >> x >>y;
            if(check(y,x)) vv.pb(2),flag = 1;
            else vv.pb(1);
            v[x].pb(y);
        }
        wt(flag + 1);
        for(auto d:vv) cout << d << " ";
        cout << endl;
    }
    return 0;
}


//#include <bits/stdc++.h>
//#include <time.h>
//#define fi first
//#define se second
//#define endll "\n"
//#define MS0(X) memset((X), 0, sizeof((X)))
//#define MS1(X) memset((X), -1, sizeof((X)))
//#define LEN(X) strlen(X)
//#define rg(a,x,y) (a+x), (a+y+1)
//#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
//#define all(x) (x).begin(),(x).end()
//#define rall(x) (x).rbegin(),(x).rend()
//#define _sum(a) accumulate(a, 0ll)
//#define _max(a,b) *max_element(a,b)
//#define pb push_back
//#define pr make_pair
//#define bugc(_) cerr << (#_) << " = " << (_) << endl;
//#define sz(x) ((int)x.size())
//#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
//#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
//#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
//#define ios  ios::sync_with_stdio(false);
//#define lson l,mid,rt << 1
//#define rson mid + 1,r,rt << 1 | 1
/////vector(len,val);
//using namespace std;
//
//typedef long long ll;
//typedef double db;
//int xx[4] = {1,-1,0,0};
//int yy[4] = {0,0,1,-1};
//const double eps = 1e-9;
//typedef pair<int,int>  P;
//typedef pair<ll,ll>  Pl;
//typedef pair<double,double>  PP;
//const int maxn = 2e6 + 5000;
//const ll mod = 1e9 + 7;
//const int INF(0x3f3f3f3f);
//const ll INFL(0x3f3f3f3f3f3f3f3fll);
//inline int sign(db a) { return a < -eps ? -1 : a > eps;}
////inline int cmp(db a,db b){ return sign(a - b);}
//
//ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
//ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
//ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
//template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
//template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
//template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
//template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
//template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
//template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
//template <typename A> inline void debug(A a){cout << a << '\n';}
//template <typename A> inline ll sqr(A x){return 1ll * x * x;}
//template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
//template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
//template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
//template<class T>void wt(const T& x){cout << x << endl;}
//template<class T>void wt(const T& x, char c){cout << x << c;}
//template<class T>void wt(const T& x, const string& s){cout << x << s;}
//template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
//template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
//inline ll lowbit(ll x){ return (x & (-x)); }
//inline void debug(){  cout << "###!!!" << endl;}
////inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
////inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
// ll n;
//Pl p[maxn];
//bool cmp(P a,P b) {
//    return a.fi - a.se >= b.fi - b.se;
//}
//ll op[33];
//ll l[maxn];
//int main() {
//    int t;
//    op[1] = 1;
//    for(int i = 2;i <= 20;i++) op[i] = op[i - 1] * 2;
//    cin >> t;
//    while(t--) {
//        string str;
//        cin >> str; ll ans = 0; int id = 0;
//        for(int i = 0;i < str.size();i++){
//                l[i] = 0;   if(str[i] == '1') l[i] = id,id = i;
//        }
//        for(int i = str.size() - 1;i >= 0;i--){
//            ll val = 0,cnt = 1;
//            if(str[i] == '1') val += 1,ans++;
//            for(int j = i - 1,k = 0;j >= 0 && k <= 20;k++,j--){
//                cnt++;
//                if(str[j] == '1') val += op[cnt];
//                if(val > cnt){
//                    int cha = val - cnt;
//                    if(l[j] - j - 1 >= cha)ans++;
//                }
//                if(val == cnt) ans++;
//            }
//            bugc(ans);
//        } wt(ans);
//    }
//    return 0;
//}
//
//

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define rg(a,x,y) (a+x), (a+y+1)
#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define _sum(a) accumulate(a, 0ll)
#define _max(a,b) *max_element(a,b)
#define pb push_back
#define pr make_pair
#define bugc(_) cerr << (#_) << " = " << (_) << endl;
#define sz(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
#define ios  ios::sync_with_stdio(false);
#define lson l,mid,rt << 1
#define rson mid + 1,r,rt << 1 | 1
///vector(len,val);
using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
typedef pair<ll,ll>  Pl;
typedef pair<double,double>  PP;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
const int INF(0x3f3f3f3f);
const ll INFL(0x3f3f3f3f3f3f3f3fll);
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
//inline int cmp(db a,db b){ return sign(a - b);}

ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline ll sqr(A x){return 1ll * x * x;}
template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template<class T>void wt(const T& x){cout << x << endl;}
template<class T>void wt(const T& x, char c){cout << x << c;}
template<class T>void wt(const T& x, const string& s){cout << x << s;}
template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
inline ll lowbit(ll x){ return (x & (-x)); }
inline void debug(){  cout << "###!!!" << endl;}
//inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
//inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
 ll n;
int main() {
    int m;ios;
    while(cin >> n >> m) {
        vector<int>v;
        for(int i = 1; i <= m; i++) {
            int x,y;
            cin >>x >>y;
            if(x < y) v.pb(1);
            else v.pb(2);
        }
        int ans = 1;set<int>s; s.clear();
        for(auto d:v) s.insert(d);
        ans = (int)s.size();
        wt(ans);
        for(auto d:v) cout << d << " ";
        cout << endl;
    }
    return 0;
}


//#include <bits/stdc++.h>
//#include <time.h>
//#define fi first
//#define se second
//#define endll "\n"
//#define MS0(X) memset((X), 0, sizeof((X)))
//#define MS1(X) memset((X), -1, sizeof((X)))
//#define LEN(X) strlen(X)
//#define rg(a,x,y) (a+x), (a+y+1)
//#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
//#define all(x) (x).begin(),(x).end()
//#define rall(x) (x).rbegin(),(x).rend()
//#define _sum(a) accumulate(a, 0ll)
//#define _max(a,b) *max_element(a,b)
//#define pb push_back
//#define pr make_pair
//#define bugc(_) cerr << (#_) << " = " << (_) << endl;
//#define sz(x) ((int)x.size())
//#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
//#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
//#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
//#define ios  ios::sync_with_stdio(false);
//#define lson l,mid,rt << 1
//#define rson mid + 1,r,rt << 1 | 1
/////vector(len,val);
//using namespace std;
//
//typedef long long ll;
//typedef double db;
//int xx[4] = {1,-1,0,0};
//int yy[4] = {0,0,1,-1};
//const double eps = 1e-9;
//typedef pair<int,int>  P;
//typedef pair<ll,ll>  Pl;
//typedef pair<double,double>  PP;
//const int maxn = 2e6 + 5000;
//const ll mod = 1e9 + 7;
//const int INF(0x3f3f3f3f);
//const ll INFL(0x3f3f3f3f3f3f3f3fll);
//inline int sign(db a) { return a < -eps ? -1 : a > eps;}
////inline int cmp(db a,db b){ return sign(a - b);}
//
//ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
//ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
//ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
//template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
//template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
//template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
//template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
//template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
//template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
//template <typename A> inline void debug(A a){cout << a << '\n';}
//template <typename A> inline ll sqr(A x){return 1ll * x * x;}
//template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
//template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
//template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
//template<class T>void wt(const T& x){cout << x << endl;}
//template<class T>void wt(const T& x, char c){cout << x << c;}
//template<class T>void wt(const T& x, const string& s){cout << x << s;}
//template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
//template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
//inline ll lowbit(ll x){ return (x & (-x)); }
//inline void debug(){  cout << "###!!!" << endl;}
////inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
////inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
// ll n;
//Pl p[maxn];
//bool cmp(P a,P b) {
//    return a.fi - a.se >= b.fi - b.se;
//}
//ll op[33];
//ll l[maxn];
//int main() {
//    int t;
//    op[1] = 1;
//    for(int i = 2;i <= 20;i++) op[i] = op[i - 1] * 2;
//    cin >> t;
//    while(t--) {
//        string str;
//        cin >> str; ll ans = 0; int id = 0;
//        for(int i = 0;i < str.size();i++){
//                l[i] = 0;   if(str[i] == '1') l[i] = id,id = i;
//        }
//        for(int i = str.size() - 1;i >= 0;i--){
//            ll val = 0,cnt = 1;
//            if(str[i] == '1') val += 1,ans++;
//            for(int j = i - 1,k = 0;j >= 0 && k <= 20;k++,j--){
//                cnt++;
//                if(str[j] == '1') val += op[cnt];
//                if(val > cnt){
//                    int cha = val - cnt;
//                    if(l[j] - j - 1 >= cha)ans++;
//                }
//                if(val == cnt) ans++;
//            }
//            bugc(ans);
//        } wt(ans);
//    }
//    return 0;
//}
//
//

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define rg(a,x,y) (a+x), (a+y+1)
#define vrg(a,x,y) (a.begin()+x),(a.begin()+y+1)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define _sum(a) accumulate(a, 0ll)
#define _max(a,b) *max_element(a,b)
#define pb push_back
#define pr make_pair
#define bugc(_) cerr << (#_) << " = " << (_) << endl;
#define sz(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define fr1(i,a,b) for (register int i=(a); i<=(b); i++)
#define fr2(i,a,b) for (register int i=(a); i>=(b); i--)
#define ios  ios::sync_with_stdio(false);
#define lson l,mid,rt << 1
#define rson mid + 1,r,rt << 1 | 1
///vector(len,val);
using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
typedef pair<ll,ll>  Pl;
typedef pair<double,double>  PP;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
const int INF(0x3f3f3f3f);
const ll INFL(0x3f3f3f3f3f3f3f3fll);
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
//inline int cmp(db a,db b){ return sign(a - b);}

ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll mulll(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline ll sqr(A x){return 1ll * x * x;}
template <typename A> A inv(A x) {return mulll(x, mod - 2,mod);}
template <typename A> void bugl(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template <typename A> void bug(A a[],int l,int r) { for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl;  }
template<class T>void wt(const T& x){cout << x << endl;}
template<class T>void wt(const T& x, char c){cout << x << c;}
template<class T>void wt(const T& x, const string& s){cout << x << s;}
template<class T>void wt(const T& x, int rnd){  cout << fixed << setprecision(rnd) << x << endl;}
template<class T> inline void read(T &x){  x=0; char c=getchar(); int f=1;  while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;}
inline ll lowbit(ll x){ return (x & (-x)); }
inline void debug(){  cout << "###!!!" << endl;}
//inline void bugl(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " "; cout << endl; }
//inline void bug(int a[],int l,int r){ for(int i = l;i <= r;i++) cout << a[i] << " ";  }
 ll n;
int main() {
    int m;ios;
    while(cin >> n >> m) {
        vector<int>v;
        for(int i = 1; i <= m; i++) {
            int x,y;
            cin >>x >>y;
            if(x < y) v.pb(1);
            else v.pb(2);
        }
        int ans = 1;set<int>s; s,clear();
        for(auto d:v) s.insert(d);
        ans = (int)s.size();
        wt(ans);
        for(auto d:v) cout << d << " ";
        cout << endl;
    }
    return 0;
}