import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import scala.language.postfixOps

object CAddOne {
  def main(args: Array[String]): Unit = {
//    val start = currentTimeMillis
    val file = new BufferedReader(new InputStreamReader(System.in))
//                    val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//        "5\n1912 1\n5 6\n999 1\n88 2\n12 100".getBytes)))
//        "1\n12 100".getBytes)))
    val out: PrintWriter = new PrintWriter(System.out)
    val modulo = 1000000007

    val cache = Array.ofDim[Long](200011)
    for (i <- 0 to 9) cache(i) = 1
    for (i <- 10 to 200010) cache(i) = (cache(i-9) + cache(i-10)) % modulo

    val t = file.readLine.toInt
    var i = 0
    while (i < t) {
      val a = file.readLine.split(" ")
      val ab = a.head.toInt
      val b = a(1).toInt
      var ints = ab
      var sum = 0L
      while (ints > 0) {
        val i = ints % 10
        sum = (sum + cache(b + i)) % modulo
        ints /= 10
      }
      out.println(sum)
      i += 1
    }
//    println(currentTimeMillis - start)
    out.close()
  }
}

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import scala.language.postfixOps

object CAddOne {
  def main(args: Array[String]): Unit = {
//    val start = currentTimeMillis
    val file = new BufferedReader(new InputStreamReader(System.in))
//                    val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//        "5\n1912 1\n5 6\n999 1\n88 2\n12 100".getBytes)))
//        "1\n12 100".getBytes)))
    val out: PrintWriter = new PrintWriter(System.out)
    val modulo: Long = 1000000007

    val cache = Array.ofDim[Long](200011)
    for (i <- 0 to 9) cache(i) = 1
    for (i <- 10 to 200010) cache(i) = (cache(i-9) + cache(i-10)) % modulo

    val t = file.readLine.toInt
    var i = 0
    while (i < t) {
      val a = file.readLine.split(" ").map(_.toLong)
      val ab = a.head
      val b = a(1).toInt
      var ints = ab
      var sum = 0L
      while (ints > 0) {
        val i = (ints % 10).toInt
        sum = (sum + cache(b + i)) % modulo
        ints /= 10
      }
      out.println(sum)
      i += 1
    }
//    println(currentTimeMillis - start)
    out.close()
  }
}

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import scala.language.postfixOps

object CAddOne {
  def main(args: Array[String]): Unit = {
//    val start = currentTimeMillis
    val file = new BufferedReader(new InputStreamReader(System.in))
//                    val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//        "5\n1912 1\n5 6\n999 1\n88 2\n12 100".getBytes)))
//        "1\n12 100".getBytes)))
    val out: PrintWriter = new PrintWriter(System.out)
    val modulo = 1000000007

    val cache = Array.ofDim[Long](200011)
    for (i <- 0 to 9) cache(i) = 1
    for (i <- 10 to 200010) cache(i) = (cache(i-9) + cache(i-10)) % modulo

    val t = file.readLine.toInt
    var i = 0
    while (i < t) {
      val a = file.readLine.split(" ").map(_.toInt)
      val ab = a.head
      val b = a(1)
      var ints = ab
      var sum = 0L
      while (ints > 0) {
        val i = ints % 10
        sum = (sum + cache(b + i)) % modulo
        ints /= 10
      }
      out.println(sum)
      i += 1
    }
//    println(currentTimeMillis - start)
    out.close()
  }
}

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.lang.System.currentTimeMillis
import scala.language.postfixOps

object CAddOne {
  val modulo: Long = (Math.pow(10,9)+7).toLong

  val cache = Array.ofDim[Long](200005)
  def computeLength(x: Int, m: Long): Long = {
    if (m + x - 10 >= 0 && cache(m.toInt + x - 10) != 0) return cache(m.toInt + x - 10)
    var r: Long = 1
    if (m + x - 10 >= 0) {
      r += computeLength(1, m + x - 10) + computeLength(0, m + x - 10) - 1 % modulo
      cache(m.toInt + x - 10) = r
    }
//    println(s"$x,$m: $r")
    r
  }

  def solve(ab: Long, b: Long): Long = {
    val a = Array.ofDim[Long](10)
    val ints = ab.toString.map(_.toString.toInt)
    var sum = 0L
    for (i <- ints) {
      if (a(i) == 0) {
        val n = computeLength(i, b) % modulo
        a(i) = n
      }
      sum = (sum  + a(i) % modulo)
//      println("sum:"+sum)
    }
//    println(cache)
    sum
  }

  def main(args: Array[String]): Unit = {
    val start = currentTimeMillis
    val file = new BufferedReader(new InputStreamReader(System.in))
//                    val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//        "5\n1912 1\n5 6\n999 1\n88 2\n12 100".getBytes)))
//        "1\n12 100".getBytes)))
    val t = file.readLine.toInt
    var i = 0
    while (i < t) {
      val a :: b :: Nil = file.readLine.split(" ").map(_.toLong).toList
      println(solve(a, b))
      i += 1
    }
    println(currentTimeMillis - start)
  }
}


import java.io._
import java.util
import java.util._

object C {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = {
    return Integer.parseInt(next)
  }

  def nextLong: Long = {
    return java.lang.Long.parseLong(next)
  }

  val MODULO = (1e9 + 7).toInt

  def sum(a: Int, b: Int): Int = {
    val sum = a + b
    val res = if (sum < MODULO) sum else sum - MODULO
    return res
  }

  def multiply(a: Int, b: Int): Int = {
    return (1L * a * b % MODULO).toInt
  }

  def calcHash(ch: Array[Char]): Int = {
    var hash = 0
    val len = ch.length
    for (i <- 0 until len) {
      hash = multiply(hash, 31)
      hash = sum(hash, (ch(i) - 'a') + 1)
    }
    return hash
  }

  class HashString(val s: Array[Char], val hash: Int) {

    def this(xs: Array[Char]) {
      this(xs, calcHash(xs))
    }

    override def hashCode(): Int = hash

    override def equals(obj: scala.Any): Boolean = {
      val s1 = obj.asInstanceOf[HashString]
      return Arrays.equals(s, s1.s)
    }
  }

  def solve: Int = {
    val n = nextInt
    val m = nextInt
    val pow = new Array[Int](6e5.toInt)
    pow(0) = 1
    for (i <- 1 until pow.length) {
      pow(i) = multiply(pow(i - 1), 31)
    }
    val set = new util.HashSet[HashString]()
    for (i <- 0 until n) {
      set.add(new HashString(next.toCharArray))
    }
    (0 until m).foreach(_ => {
      val t = next.toCharArray
      val hash = calcHash(t)
      var flag = false
      for (i <- 0 until t.length
           if !flag) {
        val prev = t(i)
        for (j <- 'a' to 'z'
             if !flag) {
          if (j != prev) {
            t(i) = j
            val updHash = sum(hash, multiply(sum(MODULO - prev, j), pow(t.length - i - 1)))
            if (set.contains(new HashString(t, updHash))) {
              flag = true
            }
          }
        }
        t(i) = prev
      }
      if (flag) {
        println("YES")
      } else {
        println("NO")
      }
    })
    return 1
  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }
}

import java.math.BigInteger
import java.util.Random

import scala.io.StdIn._

object CF291C extends App {
  val N = 600001
  val MOD = BigInteger.probablePrime(50, new Random()).longValue()
  val BASE = 4L

  val deg = new Array[Long](N)
  deg(0) = 1
  (1 until N) foreach (i => deg(i) = (deg(i - 1) * BASE) % MOD)

  def getHash(s: String): Long = {
    var hash = 0L
    (0 until s.length) foreach { i =>
      hash = (BASE * hash + (s.charAt(i) - 'a' + 1)) % MOD
    }
    hash
  }

  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val hashes = scala.collection.mutable.Set.empty[Long]
  (0 until n) foreach { i =>
    val str = readLine()
    val hash = getHash(str)
    hashes.add(hash)
  }

  (0 until m) foreach { _ =>
    val s = readLine()
    val hash = getHash(s)

    var found = false
    (0 until s.length) foreach { i =>
      List('a', 'b', 'c') foreach { c =>
        if (s.charAt(i) != c) {
          var now = (c - s.charAt(i)) * deg(s.length - 1 - i)
          now += MOD * 3
          now += hash
          now %= MOD
          if (hashes.contains(now)) {
            found = true
          }
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }
}

import java.util.Scanner

import scala.collection.mutable._

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  val cache = new HashSet[String]
  val ts = new HashSet[String]

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val memo = new ArrayBuffer[String]

    (0 until n) foreach { i =>
      val str = sc.nextLine()
      memo.append(str)
    }

    val ans = Array.tabulate[String](q)(_ => "NO")
    val queries = new HashMap[String, ArrayBuffer[Int]]

    val sizes = new HashSet[Int]
    (0 until q) foreach {idx =>
      val query = sc.nextLine()
      if (queries.contains(query)) {
        queries.get(query).get.append(idx)
      } else {
        val buff = new ArrayBuffer[Int]
        buff.append(idx)
        queries.put(query, buff)
      }
      sizes.add(query.size)
    }

    var loop = true
    for (m<-0 until memo.size if loop) {
      val str = memo(m)

      if (!cache.contains(str) && sizes.contains(str.length)) {
        if (str.length > 10000) {
          check(str, queries, ans)
        } else {
          val charBuff = str.toCharArray
          for (i <- 0 until str.length) {

            val ch = str(i)

            if (ch == 'a') {
              populate(charBuff, i, queries, ans, 'b', 'c')
            }
            if (ch == 'b') {
              populate(charBuff, i, queries, ans, 'a', 'c')
            }
            if (ch == 'c') {
              populate(charBuff, i, queries, ans, 'a', 'b')
            }

          }
        }
        cache.add(str)
      }

      if (ts.size == queries.size) {
        loop = false
      }
    }

    for( a<-0 until ans.length) {
      println(ans(a))
    }

  }

  def check(str: String, queries: HashMap[String, ArrayBuffer[Int]], ans: Array[String]): Unit = {
    queries.foreach {
      case (k,v) =>
        if (k.length == str.length) {
          var diff = 0
          var loop = true
          for (i<-0 until str.length if loop) {
            if (str(i) != k(i)) {
              diff += 1
            }
            if (diff > 1) {
              loop = false
            }
          }
          if (diff == 1) {
            v.foreach {
              idx => ans(idx) = "YES"
            }
          }
        }
    }
  }
  def populate(charBuff: Array[Char], ind: Int, queries: HashMap[String, ArrayBuffer[Int]],
               ans: Array[String], c1: Char, c2: Char): Unit = {
    val old = charBuff(ind)

    charBuff(ind) = c1
    val t1 = String.valueOf(charBuff)

    charBuff(ind) = c2
    val t2 = String.valueOf(charBuff)

    if (queries.contains(t1) && !ts.contains(t1)) {
      val buff = queries.get(t1).get
      buff.foreach { idx =>
        ans(idx) = "YES"
      }
      ts.add(t1)
    }

    if (queries.contains(t2) && !ts.contains(t2)) {
      val buff = queries.get(t2).get
      buff.foreach { idx =>
        ans(idx) = "YES"
      }
      ts.add(t2)
    }
    charBuff(ind) = old

  }

}

import java.math.BigInteger
import java.util.Random

import scala.io.StdIn._

object CF291C extends App {
  val N = 600001
  val MOD = BigInteger.probablePrime(50, new Random()).longValue()
  val BASE = 4L

  val deg = new Array[Long](N)
  deg(0) = 1
  (1 until N) foreach (i => deg(i) = (deg(i - 1) * BASE) % MOD)

  def getHash(s: String): Long = {
    var hash = 0L
    (0 until s.length) foreach { i =>
      hash = (BASE * hash + (s.charAt(i) - 'a' + 1)) % MOD
    }
    hash
  }

  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val hashes = scala.collection.mutable.Set[Long](n)
  (0 until n) foreach { i =>
    val str = readLine()
    val hash = getHash(str)
    hashes.add(hash)
  }

  (0 until m) foreach { _ =>
    val s = readLine()
    val hash = getHash(s)

    var found = false
    (0 until s.length) foreach { i =>
      List('a', 'b', 'c') foreach { c =>
        if (s.charAt(i) != c) {
          var now = (c - s.charAt(i)) * deg(s.length - 1 - i)
          now += MOD * 3
          now += hash
          now %= MOD
          if (hashes.contains(now)) {
            found = true
          }
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }

}

object CF291C extends App {
  val N = 600001
  val MOD = 1000000000000000007L
  val p = new Array[Long](N)
  p(0) = 1
  (1 until N) foreach (i => p(i) = p(i - 1) * 4 % MOD)

  def getHash(s: String): Long = {
    var hash = 0L
    (0 until s.length) foreach { i =>
      hash = (hash + (s.charAt(i) - 'a' + 1) * p(i)) % MOD
    }
    hash
  }

  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val hashes = new Array[Long](n)
  (0 until n) foreach { i =>
    val str = readLine()
    val hash = getHash(str)
    hashes(i) = hash
  }

  val sortedHashes = hashes.sorted

  (0 until m) foreach { _ =>
    val s = readLine()
    var found = false

    (0 until s.length) foreach { i =>
      List('a', 'b', 'c').toStream.takeWhile(_ => !found) foreach { c =>
        val str = new StringBuilder(s)
        str.setCharAt(i, c)
        val hash = getHash(str.toString())
        if (sortedHashes.contains(hash)) {
          found = true
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }
}

object CF291C extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val hashes = new Array[Int](n)
  (0 until n) foreach { i =>
    val str = readLine()
    val hash = str.hashCode
    hashes(i) = hash
  }

  val sortedHashes = hashes.sorted

  (0 until m) foreach { _ =>
    val s = readLine()
    var found = false

    (0 until s.length) foreach { i =>
      List('a', 'b', 'c').toStream.takeWhile(_ => !found) foreach { c =>
        val str = new StringBuilder(s)
        str.setCharAt(i, c)
        val hash = str.toString().hashCode()
        if (sortedHashes.contains(hash)) {
          found = true
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }
}

object CF291C extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val set = scala.collection.mutable.Set.empty[Int]
  (0 until n) foreach { _ =>
    val s = readLine().hashCode
    set += s
  }

  (0 until m) foreach { _ =>
    val s = readLine()
    var found = false

    (0 until s.length) foreach { i =>
      List('a', 'b', 'c').toStream.takeWhile(_ => !found) foreach { c =>
        val str = new StringBuilder(s)
        str.setCharAt(i, c)
        val hash = str.toString().hashCode()
        if (set.contains(hash)) {
          found = true
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }
}

import java.math.BigInteger
import java.util.Random

import scala.io.StdIn._

object CF291C extends App {
  val N = 600001
  val MOD = BigInteger.probablePrime(51, new Random()).longValue()
  val BASE = 4L

  val deg = new Array[Long](N)
  deg(0) = 1
  (1 until N) foreach (i => deg(i) = (deg(i - 1) * BASE) % MOD)

  def getHash(s: String): Long = {
    var hash = 0L
    (0 until s.length) foreach { i =>
      hash = (BASE * hash + (s.charAt(i) - 'a' + 1)) % MOD
    }
    hash
  }

  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val hashes = scala.collection.mutable.Set[Long](n)
  (0 until n) foreach { i =>
    val str = readLine()
    val hash = getHash(str)
    hashes.add(hash)
  }

  (0 until m) foreach { _ =>
    val s = readLine()
    val hash = getHash(s)

    var found = false
    (0 until s.length) foreach { i =>
      List('a', 'b', 'c') foreach { c =>
        if (s.charAt(i) != c) {
          var now = (c - s.charAt(i)) * deg(s.length - 1 - i)
          now += MOD * 3
          now += hash
          now %= MOD
          if (hashes.contains(now)) {
            found = true
          }
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }

}

object CF291C extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val set = scala.collection.mutable.Set.empty[String]
  (0 until n) foreach { _ =>
    val s = readLine()
    set += s
  }

  (0 until m) foreach { _ =>
    val s = readLine()
    var found = false

    (0 until s.length) foreach { i =>
      List('a', 'b', 'c').toStream.takeWhile(_ => !found) foreach { c =>
        val str = new StringBuilder(s)
        str.setCharAt(i, c)
        if (set.contains(str.toString())) {
          found = true
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }
}

import scala.io.StdIn._

object CF291C extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  def fits(s1: String, s2: String): Boolean = {
    if (s1.length != s2.length) {
      false
    }

    var n = 0
    (0 until s1.length) foreach { i =>
      if (s1.charAt(i) != s2.charAt(i)) n += 1
      if (n > 1) false
    }

    true
  }

  val CUTOFF = 700
  val small = scala.collection.mutable.Set.empty[String]
  val big = scala.collection.mutable.Set.empty[String]
  (0 until n) foreach { _ =>
    val str = readLine()
    if (str.length < CUTOFF) small.add(str)
    else big.add(str)
  }

  (0 until m) foreach { _ =>
    val s = readLine()

    if (s.length < CUTOFF) {
      var found = false

      (0 until s.length) foreach { i =>
        List('a', 'b', 'c') foreach { c =>
          if (s.charAt(i) != c) {
            val str = new StringBuilder(s)
            str.setCharAt(i, c)
            if (small.contains(str.toString())) {
              found = true
            }
          }
        }
      }

      if (found) println("YES")
      else println("NO")

    } else {
      var found = false
      big.toStream.takeWhile(_ => !found) foreach { str =>
        if (fits(s, str)) found = true
      }
      if (found) println("YES")
      else println("NO")
    }
  }
}

object CF291C extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val hashes = new Array[String](n)
  (0 until n) foreach { i =>
    val str = readLine()
    val hash = str.hashCode
    hashes(i) = str
  }

  val sortedHashes = hashes.sorted

  (0 until m) foreach { _ =>
    val s = readLine()
    var found = false

    (0 until s.length) foreach { i =>
      List('a', 'b', 'c').toStream.takeWhile(_ => !found) foreach { c =>
        val str = new StringBuilder(s)
        str.setCharAt(i, c)
        val hash = str.toString().hashCode()
        if (sortedHashes.contains(str.toString())) {
          found = true
        }
      }
    }

    if (found) println("YES")
    else println("NO")
  }
}

import java.io._
import java.util
import java.util._

object C {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = {
    return Integer.parseInt(next)
  }

  def nextLong: Long = {
    return java.lang.Long.parseLong(next)
  }

  val MODULO = (1e7 + 9).toInt

  def sum(a: Int, b: Int): Int = {
    val sum = a + b
    val res = if (sum < MODULO) sum else sum - MODULO
    return res
  }

  def multiply(a: Int, b: Int): Int = {
    return (1L * a * b % MODULO).toInt
  }

  def calcHash(ch: Array[Char]): Int = {
    var hash = 0
    val len = ch.length
    for (i <- 0 until len) {
      hash = multiply(hash, 31)
      hash = sum(hash, ch(i) - 'a' + 1)
    }
    return hash
  }

  class HashString(val s: Array[Char], val hash: Int) {

    def this(xs: Array[Char]) {
      this(xs, calcHash(xs))
    }

    override def hashCode(): Int = hash

    override def equals(obj: scala.Any): Boolean = {
      val s1 = obj.asInstanceOf[HashString]
      return Arrays.equals(s, s1.s)
    }
  }

  def solve: Int = {
    val n = nextInt
    val m = nextInt
    val pow = new Array[Int](3e5.toInt)
    pow(0) = 1
    for (i <- 1 until pow.length) {
      pow(i) = multiply(pow(i - 1), 31)
    }
    val set = new util.HashSet[HashString]()
    for (i <- 0 until n) {
      set.add(new HashString(next.toCharArray))
    }
    (0 until m).foreach(_ => {
      val t = next.toCharArray
      val hash = calcHash(t)
      var i = 0
      var flag = true
      while (flag && i < t.length) {
        val c = t(i)
        for (j <- 'a' to 'z') {
          if (j != c) {
            t(i) = j
            val updHash = sum(hash, multiply(sum(MODULO - c, c), pow(t.length - i - 1)))
            if (set.contains(new HashString(t, updHash))) {
              flag = false
            }
          }
        }
        t(i) = c
        i += 1
      }
      if (flag) {
        println("YES")
      } else {
        println("NO")
      }
    })
    return 1
  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }
}

import java.util.Scanner

import scala.collection.mutable.HashMap

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val mapN = new HashMap[Int, (Int,Int,Int)]
    (0 until n) foreach { i =>

      val str = sc.nextLine()
      val as = str.filter(c => c == 'a').size
      val bs = str.filter(c => c == 'b').size
      val cs = str.filter(c => c == 'c').size
      mapN.put(i, (as,bs,cs))
    }

    (0 until q) foreach {_ =>
      val query = sc.nextLine()

      val qa = query.filter(c => c == 'a').size
      val qb = query.filter(c => c == 'b').size
      val qc = query.filter(c => c == 'c').size

      var loop = true
      var ans = "NO"
      for (i<-0 until n if loop) {
        val out = mapN.get(i).get
        if ((math.abs(out._1 - qa) + math.abs(out._2 - qb) + math.abs(out._3 - qc)) <= 2 && qa + qb + qc == out._1 + out._2 + out._3) {
          ans = "YES"
          loop = false
        }
      }

      println(ans)

    }

  }

}

import java.util.Scanner

import scala.collection.mutable._

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val mapN = new HashMap[Int, ArrayBuffer[(Int,Int,Int, String)]]
    (0 until n) foreach { i =>

      val str = sc.nextLine()
      val as = str.filter(c => c == 'a').size
      val bs = str.filter(c => c == 'b').size
      val cs = str.filter(c => c == 'c').size
      val l = str.length
      if (mapN.contains(l)) {
        mapN.get(l).get.append((as,bs,cs, str))
      } else {
        val buff = new ArrayBuffer[(Int,Int,Int, String)]
        buff.append((as, bs, cs, str))
        mapN.put(l, buff)
      }
    }

    val cache = new HashMap[String, String]
    (0 until q) foreach {_ =>
      val query = sc.nextLine()

      if (cache.contains(query)) {
        println(cache.get(query).get)
      } else {

        var qa = 0
        var qb = 0
        var qc = 0

        query.foreach { j =>
          if (j == 'a') qa += 1
          if (j == 'b') qb += 1
          if (j == 'c') qc += 1
        }

        val sub = mapN.get(query.length).getOrElse(null)
        var loop = true
        var ans = "NO"

        if (sub != null) {
          for (i <- 0 until sub.length if loop) {
            val out = sub(i)
            val diff = (math.abs(out._1 - qa) + math.abs(out._2 - qb) + math.abs(out._3 - qc))

            if ((diff == 2 || diff == 1) && areEq(out._4, query)) {
              ans = "YES"
              loop = false
            }
          }
        }
        cache.put(query, ans)
        println(ans)
      }

    }

  }

  def areEq(s: String, q: String): Boolean = {
    var diff = 0
    var loop = true
    val mid = s.length/2 - 1
    for (i <- 0 to mid if loop) {
      if (s(i) != q(i) || s(s.length-i-2) != q(q.length -i -2)) {
        diff += 1
      }
      if (diff > 1)
        loop = false
    }

    return diff == 1
  }

}

import java.util.Scanner

import scala.collection.mutable._

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val memo = new ArrayBuffer[String]

    (0 until n) foreach { i =>
      val str = sc.nextLine()
      memo.append(str)
    }

    val ans = Array.tabulate[String](q)(_ => "NO")
    val queries = new HashMap[String, Int]

    (0 until q) foreach {idx =>
      val query = sc.nextLine()
      queries.put(query, idx)
    }

    memo.foreach {str =>

      for (i<- 0 until str.length) {

        val ch = str(i)
        val pre = if (i == 0) "" else str.substring(0,i)
        val suffix = str.substring(i+1)

        if (ch == 'a') {
          populate(pre, suffix, queries,ans, 'b','c')
        }
        if (ch == 'b') {
          populate(pre, suffix, queries,ans, 'a', 'c')
        }
        if (ch == 'c') {
          populate(pre, suffix, queries,ans, 'a', 'b')
        }
      }
    }

    for( a<-0 until ans.length) {
      println(ans(a))
    }

  }

  def populate(pre: String, suffix: String, queries: HashMap[String, Int],
               ans: Array[String], c1: Char, c2: Char): Unit = {
    val t1 = pre+c1+suffix
    val t2 = pre+c2+suffix
    if (queries.contains(t1)) {
      ans(queries.get(t1).get) = "YES"
    }
    if (queries.contains(t2)) {
      ans(queries.get(t2).get) = "YES"
    }
  }

}

import java.util.Scanner

import scala.collection.mutable._

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val mapN = new HashMap[Int, ArrayBuffer[(Int,Int,Int, String)]]
    (0 until n) foreach { i =>

      val str = sc.nextLine()
      val as = str.filter(c => c == 'a').size
      val bs = str.filter(c => c == 'b').size
      val cs = str.filter(c => c == 'c').size
      val l = str.length
      if (mapN.contains(l)) {
        mapN.get(l).get.append((as,bs,cs, str))
      } else {
        val buff = new ArrayBuffer[(Int,Int,Int, String)]
        buff.append((as, bs, cs, str))
        mapN.put(l, buff)
      }
    }

    val cache = new HashMap[String, String]
    (0 until q) foreach {_ =>
      val query = sc.nextLine()

      if (cache.contains(query)) {
        println(cache.get(query).get)
      } else {

        var qa = 0
        var qb = 0
        var qc = 0

        query.foreach { j =>
          if (j == 'a') qa += 1
          if (j == 'b') qb += 1
          if (j == 'c') qc += 1
        }

        val sub = mapN.get(query.length).getOrElse(null)
        var loop = true
        var ans = "NO"

        if (sub != null) {
          for (i <- 0 until sub.length if loop) {
            val out = sub(i)
            val diff = (math.abs(out._1 - qa) + math.abs(out._2 - qb) + math.abs(out._3 - qc))

            val adiff = math.abs(out._1 - qa)
            val bdiff = math.abs(out._2 - qb)
            val cdiff = math.abs(out._2 - qc)

            if ((diff == 2 || diff == 1) && !(adiff > 1) && !(bdiff > 1) && !(cdiff > 1) && areEq(out._4, query)) {
              ans = "YES"
              loop = false
            }
          }
        }
        cache.put(query, ans)
        println(ans)
      }

    }

  }

  def areEq(s: String, q: String): Boolean = {
    var diff = 0
    var loop = true
    for (i <- 0 until s.length if loop) {
      if (s(i) != q(i)) {
        diff += 1
      }
      if (diff > 1)
        loop = false
    }

    return diff == 1
  }

}

import java.util.Scanner

import scala.collection.mutable.HashMap

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val mapN = new HashMap[Int, (Int,Int,Int)]
    (0 until n) foreach { i =>

      val str = sc.nextLine()
      val as = str.filter(c => c == 'a').size
      val bs = str.filter(c => c == 'b').size
      val cs = str.filter(c => c == 'c').size
      mapN.put(i, (as,bs,cs))
    }

    (0 until q) foreach {_ =>
      val query = sc.nextLine()

      val qa = query.filter(c => c == 'a').size
      val qb = query.filter(c => c == 'b').size
      val qc = query.filter(c => c == 'c').size

      var loop = true
      var ans = "NO"
      for (i<-0 until n if loop) {
        val out = mapN.get(i).get
        if ((math.abs(out._1 - qa) + math.abs(out._2 - qb) + math.abs(out._3 - qc)) == 2 && qa + qb + qc == out._1 + out._2 + out._3) {
          ans = "YES"
          loop = false
        }
      }

      println(ans)

    }

  }

}

import java.util.Scanner

import scala.collection.mutable._

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  val cache = new HashSet[String]

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val memo = new ArrayBuffer[String]

    (0 until n) foreach { i =>
      val str = sc.nextLine()
      memo.append(str)
    }

    val ans = Array.tabulate[String](q)(_ => "NO")
    val queries = new HashMap[String, ArrayBuffer[Int]]

    (0 until q) foreach {idx =>
      val query = sc.nextLine()
      if (queries.contains(query)) {
        queries.get(query).get.append(idx)
      } else {
        val buff = new ArrayBuffer[Int]
        buff.append(idx)
        queries.put(query, buff)
      }

    }

    memo.foreach {str =>

      if (!cache.contains(str)) {
        for (i <- 0 until str.length) {

          val ch = str(i)
          val pre = if (i == 0) "" else str.substring(0, i)
          val suffix = str.substring(i + 1)

          if (ch == 'a') {
            populate(pre, suffix, queries, ans, 'b', 'c')
          }
          if (ch == 'b') {
            populate(pre, suffix, queries, ans, 'a', 'c')
          }
          if (ch == 'c') {
            populate(pre, suffix, queries, ans, 'a', 'b')
          }
        }
        cache.add(str)
      }
    }

    for( a<-0 until ans.length) {
      println(ans(a))
    }

  }

  def populate(pre: String, suffix: String, queries: HashMap[String, ArrayBuffer[Int]],
               ans: Array[String], c1: Char, c2: Char): Unit = {
    val t1 = pre+c1+suffix
    val t2 = pre+c2+suffix
    if (queries.contains(t1)) {
      val buff = queries.get(t1).get
      buff.foreach { idx =>
        ans(idx) = "YES"
      }
      cache.add(t1)
    }
    if (queries.contains(t2)) {
      val buff = queries.get(t2).get
      buff.foreach { idx =>
        ans(idx) = "YES"
      }
      cache.add(t2)
    }

  }

}

import java.util.Scanner

import scala.collection.mutable.HashMap

/**
 * Created by dimitar on 2/14/15.
 */
object Queries {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val q = sc.nextInt()

    sc.nextLine()
    val mapN = new HashMap[Int, (Int,Int,Int)]
    (0 until n) foreach { i =>

      val str = sc.nextLine()
      val as = str.filter(c => c == 'a').size
      val bs = str.filter(c => c == 'b').size
      val cs = str.filter(c => c == 'c').size
      mapN.put(i, (as,bs,cs))
    }

    (0 until q) foreach {_ =>
      val query = sc.nextLine()

      val qa = query.filter(c => c == 'a').size
      val qb = query.filter(c => c == 'b').size
      val qc = query.filter(c => c == 'c').size

      var loop = true
      var ans = "NO"
      for (i<-0 until n if loop) {
        val out = mapN.get(i).get
        val diff = (math.abs(out._1 - qa) + math.abs(out._2 - qb) + math.abs(out._3 - qc))
        if ( (diff == 1 || diff == 2) && qa + qb + qc == out._1 + out._2 + out._3) {
          ans = "YES"
          loop = false
        }
      }

      println(ans)

    }

  }

}


object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    sort(A)

    var ans = 0L
    REP(N / 2) { i =>
      val s = A(i) + A(N - 1 - i)
      ans += s.toLong * s
    }

    out.println(ans)
  }

  def sort(a: Array[Int]): Array[Int] = {
    val n = a.length
    REP(n) { i =>
      val j = scala.util.Random.nextInt(n - i) + i
      val tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
    }
    Sorting.stableSort(a)
    a
  }

  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  def debug(num: Long): Unit = {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import java.util.Scanner

object Main {

  def main(args: Array[String]): Unit = {
    val scan = new Scanner(System.in)
    val n = scan.nextInt
    scan.nextLine
    val xs: Array[Int] = (scan.nextLine split ' ' map (_.toInt)).sorted
    val ans = (xs.take(n / 2) zip xs.drop(n / 2).reverse map { case (x, y) => (x + y).toLong * (x + y) }).sum
    println(ans)
  }
}


import scala.io.StdIn._
object Main extends App {

  val MOD = 1000000007
  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val maxSize = n + 2 * m - 1
  val factorial = Array.fill(maxSize + 1){1L}
  val inv = Array.fill(maxSize + 1){1L}
  val invFactorial = Array.fill(maxSize + 1){1L}
  for (i ← 2 to maxSize) {
    factorial(i) = factorial(i - 1) * i % MOD
    inv(i) = (MOD - MOD / i) * inv(MOD % i) % MOD
    invFactorial(i) = inv(i) * invFactorial(i - 1) % MOD
  }
  println(
    factorial(maxSize) * invFactorial(n - 1) % MOD * invFactorial(2 * m) % MOD
  )
}

import java.io.BufferedInputStream
import java.util.Scanner

object Main extends App {
  val inputReader = new Scanner(new BufferedInputStream(System.in))
  val MOD = 1000000007L;
  val N = inputReader.nextInt()
  val M = inputReader.nextInt()
  val end_with = Array.ofDim[Long](N + 1, M + 1)
  val start_with = Array.ofDim[Long](N + 1, M + 1)
  for (k <- 1 to N) {
    end_with(k).update(1, 1)
    start_with(k).update(1, 1)
  }

  for (m <- 2 to M) {
    for (k <- 1 to N) {
      var sum = 0L;
      for (i <- 0 until k) {
        sum += end_with(k - i)(m - 1)
      }
      end_with(k).update(m, sum % MOD)
    }
  }

  for (m <- 2 to M) {
    for (k <- 1 to N) {
      var sum = 0L;
      for (i <- k to N) {
        sum += start_with(i)(m - 1)
      }
      start_with(k).update(m, sum % MOD)
    }
  }

  var result = 0L
  for (k <- 1 to N) {
    var start_at_least_k = 0L
    for (i <- k to N) {
      start_at_least_k += start_with(i)(M)
    }
    result = result + (end_with(k)(M) * (start_at_least_k % MOD)) % MOD
  }
  println(result % MOD)
}

import java.io.BufferedInputStream
import java.util.Scanner

object Main extends App {
  val inputReader = new Scanner(new BufferedInputStream(System.in))
  val MOD = 1000000007L;
  val N = inputReader.nextInt()
  val M = inputReader.nextInt()
  val end_with = Array.ofDim[Long](M + 1, N + 1)
  val start_with = Array.ofDim[Long](M + 1, N + 1)
  for (k <- 1 to N) {
    end_with(1).update(k, 1)
    start_with(1).update(k, 1)
  }

  for (m <- 2 to M) {
    for (k <- 1 to N) {
      val sum = end_with(m - 1).slice(1, k + 1).sum
      end_with(m).update(k, sum % MOD)
    }
  }

  for (m <- 2 to M) {
    for (k <- 1 to N) {
      val sum = start_with(m - 1).slice(k, N + 1).sum
      start_with(m).update(k, sum % MOD)
    }
  }

  var result = 0L
  for (k <- 1 to N) {
    val start_at_least_k = start_with(M).slice(k, N + 1).sum
    result = result + (end_with(M)(k) * (start_at_least_k % MOD)) % MOD
  }
  println(result % MOD)
}

object Main {
    def main(args: Array[String]) = {
      val Array(n, m) = readLine.split(" ").map(_.toInt)
      println(f1(n, m))
    }
    def f1(n: Int, m: Int): Long = {
        val a = Array.ofDim[Long](n + 1, m)
        for(j <- 0 to n) {
            a(j)(0) = 1
        }
        for (i <- 1 until m) {
            for (j <- 0 to n) {
                for (k <- 0 to j) {
                    a(j)(i) = (a(j)(i) + a(k)(i - 1)) % 1000000007L
                }
            }
        }
        val x = (for (
            i <- 1 to n;
            j <- 1 to n) yield (i, j)
        ).filter{ case (x, y) => x <= y }
        val s = x.foldLeft(0L){ case (s, (x, y)) =>
            (s + a(x - 1)(m-1) * a(n - y)(m-1)) % 1000000007L
        }
        s
    }
    def countSum(x: Int, m: Int) = {
        val a = Array.ofDim[Int](x + 1, m)
        for(j <- 1 to x) {
            a(j)(0) = 1
        }
        for (i <- 1 until m) {
            for (j <- 1 to x) {
                for (k <- 1 until j) {
                    a(j)(i) += a(k)(i - 1)
                }
            }
        }
        // println(a.map(_.mkString(" ")).mkString("\n"))
        val res = a(x)(m - 1)
        // println((x, m, res))
        res
    }
    def powL(x: Int, p: Int): Long = {
        if (p <= 0) 1L else (x.toLong * powL(x, p - 1)) % 1000000007L
    }
}


object A {
  import InOut._
  def main(args: Array[String]): Unit = {}

  val t = nextInt
  for (_ <- 1 to t) {
    val n = nextInt
    val as, bs = nextInts(n)
    val l = (0 until n).indexWhere(i => as(i) != bs(i))
    val r = (0 until n).lastIndexWhere(i => as(i) != bs(i))
    val ok = if (l >= 0) {
      val d = bs(l) - as(l)
      d > 0 && (l to r).forall(i => bs(i) - as(i) == d)
    } else true

    out.println(if (ok) "YES" else "NO")
    out.flush()
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object A1253 {

  import IO._
  import collection.{mutable => cu}
  import scala.collection.mutable.ArrayBuffer

  def main(args: Array[String]): Unit = {
    var tests = readInt
    while (tests > 0) {
      val Array(n) = readInts(1)
      val a = readInts(n)
      val b = readInts(n)
      for (i <- 0 until n)
        b(i) -= a(i)
      if (b.exists(_ < 0))
        out.println("NO")
      else if (b.forall(_ == 0))
        out.println("YES")
      else if (b.distinct.count(_ > 0) == 1) {
        var idxs = b.zipWithIndex.filter(_._1 != 0).map(_._2)
        if (idxs.length == (idxs.last - idxs.head + 1)) {
          out.println("YES")
        } else {
          out.println("NO")
        }
      } else {
        out.println("NO")
      }

      tests -= 1
    }
    out.close()
  }

  object IO {
    private val input = new java.io.BufferedReader(
      new java.io.InputStreamReader(
        if (System.getProperty("ONLINE_JUDGE") != null) System.in
        else new java.io.FileInputStream("src/input")
      )
    )
    val out = new java.io.PrintWriter(System.out)

    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}

object A1253 {

  import IO._
  import collection.{mutable => cu}
  import scala.collection.mutable.ArrayBuffer

  def main(args: Array[String]): Unit = {
    var tests = readInt
    while (tests > 0) {
      val Array(n) = readInts(1)
      val a = readInts(n)
      val b = readInts(n)
      for (i <- 0 until n)
        b(i) -= a(i)
      if (b.exists(_ < 0))
        out.println("NO")
      else {
        if (b.distinct.count(_ > 0) == 0)
          out.println("YES")
        else if (b.distinct.count(_ > 0) != 1)
          out.println("NO")
        else {
          var res = 0
          var i = 1
          var change = 0
          while (i < n) {
            if (b(i) != b(i - 1)) {
              change += 1
            }
            i += 1
          }
          if (change > 2)
            out.println("NO")
          else
            out.println("YES")
        }
      }

      tests -= 1
    }
    out.close()
  }

  object IO {
    private val input = new java.io.BufferedReader(
      new java.io.InputStreamReader(
        if (System.getProperty("ONLINE_JUDGE") != null) System.in
        else new java.io.FileInputStream("src/input")
      )
    )
    val out = new java.io.PrintWriter(System.out)

    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}


import scala.collection.mutable

object D {

  def main(args: Array[String]): Unit = {}
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)

  val precedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val res = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    res(i) = if (u > v) 2 else 1
    precedingBuilders(v - 1) += u - 1
  }

  val preceding = precedingBuilders.map(_.result)

  var hasCycle = false
  val color = Array.fill(n)(0)

  def dfs0(u: Int): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceding(u).length) {
      val v = preceding(u)(i)
      if (color(v) == 0) dfs0(v)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i)

  if (hasCycle) {
    System.out.println(2)
    System.out.println(res.mkString(" "))
  } else {
    System.out.println(1)
    System.out.println(Array.fill(m)(1).mkString(" "))
  }
}

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  def solve(): Unit = {
    val N, M = ni()
    val (from, to) = na2(M, -1)
    val g = packDGraph(N, from, to)
    if (topologicalSort(g) != null) {
      out.println(1)
      val ans = map(M){_ => 1 }
      out.println(ans.mkString(" "))
    } else {
      out.println(2)
      val ans = Array.ofDim[Int](M)
      REP(M) { i =>
        ans(i) = if (from(i) < to(i)) 1 else 2
      }
      out.println(ans.mkString(" "))
    }
  }

  def packDGraph(n: Int, from: Array[Int], to: Array[Int]): Array[Array[Int]] = {
    val t = new Array[Array[Int]](n)
    val p = new Array[Int](n)
    val m = from.length
    REP(m)(i => p(from(i)) += 1)
    REP(n)(i => t(i) = new Array(p(i)))
    REP_r(m) { i => // 順序維持する
      p(from(i)) -= 1
      t(from(i))(p(from(i))) = to(i)
    }
    t
  }
  /**
    * @return 閉路の場合はnull
    */
  def topologicalSort(g: Array[Array[Int]]): Array[Int] = {
    val n = g.length
    val L = Array.ofDim[Int](n)
    var ptr = 0
    val S = Array.ofDim[Int](n)
    var last, cur = 0
    val deg = Array.ofDim[Int](n)
    REP(n) { i =>
      REP(g(i).length) { j =>
        deg(g(i)(j)) += 1
      }
    }

    REP(n) { i =>
      if (deg(i) == 0) {
        S(last) = i
        last += 1
      }
    }

    while(cur < last) {
      val v = S(cur)
      cur += 1
      L(ptr) = v
      ptr += 1

      REP(g(v).length) { i =>
        val u = g(v)(i)
        deg(u) -= 1
        if (deg(u) == 0) {
          S(last) = u
          last += 1
        }
      }
    }

    // 閉路チェック
    REP(n) { i =>
      if (deg(i) > 0) return null
    }
    L
  }
}

import scala.collection.mutable

object D {

  def main(args: Array[String]): Unit = {}
  val out = new java.io.PrintWriter(System.out)
  val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
  def tokenizeLine = new java.util.StringTokenizer(in.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)

  val precedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val res = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    res(i) = if (u > v) 2 else 1
    precedingBuilders(v - 1) += u - 1
  }

  val preceding = precedingBuilders.map(_.result)

  var hasCycle = false
  val color = Array.fill(n)(0)

  def dfs0(u: Int): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceding(u).length) {
      val v = preceding(u)(i)
      if (color(v) == 0) dfs0(v)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i)

  if (hasCycle) {
    System.out.println(2)
    System.out.println(res.mkString(" "))
  } else {
    System.out.println(1)
    System.out.println(Array.fill(m)(1).mkString(" "))
  }
}

import scala.collection.mutable

object D {

  def main(args: Array[String]): Unit = {}
  val out = new java.io.PrintWriter(System.out)
  val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
  def tokenizeLine = new java.util.StringTokenizer(in.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)

  val precedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val res = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    res(i) = if (u > v) 2 else 1
    precedingBuilders(v - 1) += u - 1
  }

  val preceding = precedingBuilders.map(_.result)

  var hasCycle = false
  val color = Array.fill(n)(0)

  def dfs0(u: Int): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceding(u).length) {
      val v = preceding(u)(i)
      if (color(v) == 0) dfs0(v)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i)

  if (hasCycle) {
    out.println(2)
    out.println(res.mkString(" "))
  } else {
    out.println(1)
    out.println(Array.fill(m)(1).mkString(" "))
  }

  out.flush()
}

import scala.collection.mutable

object D {

  def main(args: Array[String]): Unit = {}
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  val Array(n, m) = readInts(2)

  val precedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val res = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    res(i) = if (u > v) 2 else 1
    precedingBuilders(v - 1) += u - 1
  }

  val preceding = precedingBuilders.map(_.result)

  var hasCycle = false
  val color = Array.fill(n)(0)

  def dfs0(u: Int): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceding(u).length) {
      val v = preceding(u)(i)
      if (color(v) == 0) dfs0(v)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i)

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    if (edge >= 0) sortedBuilder += edge
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  val sorted = sortedBuilder.result

//println(sorted.mkString(" "))
  for (i <- sorted.indices) res(sorted(i)) = 1 + i % 2

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(u - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(n)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, 0, step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

//  val sorted = sortedBuilder.result
//
//  val order = Array.ofDim[Int](n)
//  for (i <- sorted.indices) {
//    order(sorted(i)) = 1 + i % 2
//  }

  //println(sorted.map(_ + 1).mkString(" "))
  //println(order.mkString(" "))
  //for (i <- 0 until m) res(i) = order(edgeTo(i))

  val visited = Array.fill(n)(false)

  def dfs1(u: Int, u0: Int, step: Int): Unit = {
    /*if (step > 0 && u == u0) hasCycle = true
    else */if (!visited(u)) {
      visited(u) = true
      var i = 0
      while (i < adjs(u).length) {
        val edge = adjs(u)(i)
        if (!visited(edgeTo(edge))) {
          res(edge) = step % 2 + 1
          dfs1(edgeTo(edge), u0, step + 1)
        }
        i += 1
      }
    }
  }
//
//  var iter = 0
  for (u <- 0 until n) {
    if (!visited(u)) {
      dfs1(u, u, 0)
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    //if (edge >= 0) sortedBuilder += edge
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  //val sorted = sortedBuilder.result

//println(sorted.mkString(" "))
  //for (i <- sorted.indices) res(sorted(i)) = 1 + i % 2

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = u - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  val sorted = sortedBuilder.result

  val order = Array.ofDim[Int](n)
  for (i <- sorted.indices) {
    order(sorted(i)) = 1 + i % 2
  }

  //println(sorted.mkString(" "))
  //println(order.mkString(" "))
  for (i <- 0 until m) res(i) = order(edgeTo(i))

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = u - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  val sorted = sortedBuilder.result

  val order = Array.ofDim[Int](n)
  for (i <- sorted.indices) {
    order(sorted(i)) = 1 + i % 2
  }

  println(sorted.mkString(" "))
  println(order.mkString(" "))
  for (i <- 0 until m) res(i) = order(edgeTo(i))

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D {

  def main(args: Array[String]): Unit = {}
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(u - 1) += i
  }

  val adjs = adjBuilders.map(_.result)

  var hasCycle = false
  val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)

  def dfs1(u: Int, u0: Int, step: Int): Unit = {
    if (step > 0 && u == u0) hasCycle = true
    else if (iters(u) == 0) {
      iters(u) = iter
      var i = 0
      while (i < adjs(u).length) {
        val edge = adjs(u)(i)
        color(edge) = step % 2 + 1
        dfs1(edgeTo(edge), u0, step + 1)
        i += 1
      }
    }
  }

  var iter = 0
  for (u <- 0 until n) {
    if (iters(u) == 0) {
      iter += 1
      dfs1(u, u, 0)
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(color.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  val sorted = sortedBuilder.result

//println(sorted.mkString(" "))
  val order = Array.ofDim[Int](n)
  for (i <- sorted.indices) {
    order(sorted(i)) = 1 + i % 2
  }

  for (i <- 0 until m) res(i) = order(edgeTo(i))

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(u - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)

  def dfs0(u: Int): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      if (color(v) == 0) dfs0(v)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i)

  val res = Array.fill(m)(0)

  def dfs1(u: Int, u0: Int, step: Int): Unit = {
    /*if (step > 0 && u == u0) hasCycle = true
    else */if (iters(u) == 0) {
      iters(u) = iter
      var i = 0
      while (i < adjs(u).length) {
        val edge = adjs(u)(i)
        res(edge) = step % 2 + 1
        dfs1(edgeTo(edge), u0, step + 1)
        i += 1
      }
    }
  }

  var iter = 0
  for (u <- 0 until n) {
    if (iters(u) == 0) {
      iter += 1
      dfs1(u, u, 0)
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = u - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(n)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  val sorted = sortedBuilder.result

  val order = Array.ofDim[Int](n)
  for (i <- sorted.indices) {
    order(sorted(i)) = 1 + i % 2
  }

  //println(sorted.mkString(" "))
  //println(order.mkString(" "))
  for (i <- 0 until m) res(i) = order(edgeTo(i))

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(u - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  val iters = Array.fill(n)(0)
  val color = Array.fill(m)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  def dfs0(u: Int, edge: Int): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      if (color(v) == 0) dfs0(v, adjs(u)(i))
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    if (edge >= 0) sortedBuilder += edge
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

  val sorted = sortedBuilder.result

  val res = Array.fill(m)(1)
//println(sorted.mkString(" "))
  for (i <- sorted.indices) res(sorted(i)) = 1 + i % 2

  //
//  def dfs1(u: Int, u0: Int, step: Int): Unit = {
//    /*if (step > 0 && u == u0) hasCycle = true
//    else */if (iters(u) == 0) {
//      iters(u) = iter
//      var i = 0
//      while (i < adjs(u).length) {
//        val edge = adjs(u)(i)
//        res(edge) = step % 2 + 1
//        dfs1(edgeTo(edge), u0, step + 1)
//        i += 1
//      }
//    }
//  }
//
//  var iter = 0
//  for (u <- 0 until n) {
//    if (iters(u) == 0) {
//      iter += 1
//      dfs1(u, u, 0)
//    }
//  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = v - 1
    adjBuilders(u - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(n)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, 0, step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

//  val sorted = sortedBuilder.result
//
//  val order = Array.ofDim[Int](n)
//  for (i <- sorted.indices) {
//    order(sorted(i)) = 1 + i % 2
//  }

  //println(sorted.map(_ + 1).mkString(" "))
  //println(order.mkString(" "))
  //for (i <- 0 until m) res(i) = order(edgeTo(i))

  val visited = Array.fill(n)(false)

  def dfs1(u: Int, u0: Int, step: Int): Unit = {
    /*if (step > 0 && u == u0) hasCycle = true
    else */if (!visited(u)) {
      visited(u) = true
      var i = 0
      while (i < adjs(u).length) {
        val edge = adjs(u)(i)
        res(edge) = step % 2 + 1
        dfs1(edgeTo(edge), u0, step + 1)
        i += 1
      }
    }
  }
//
//  var iter = 0
  for (u <- 0 until n) {
    if (!visited(u)) {
      dfs1(u, u, 0)
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}

import scala.collection.mutable

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  val adjBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val preceedingBuilders = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val edgeTo = Array.ofDim[Int](m)

  for (i <- 0 until m) {
    val Array(u, v) = readInts(2)
    edgeTo(i) = u - 1
    adjBuilders(v - 1) += i
    preceedingBuilders(v - 1) += u - 1
  }

  val adjs = adjBuilders.map(_.result)
  val preceeding = preceedingBuilders.map(_.result)

  var hasCycle = false
  //val iters = Array.fill(n)(0)
  val color = Array.fill(n)(0)
  val sortedBuilder = new mutable.ArrayBuilder.ofInt

  val res = Array.fill(m)(1)

  def dfs0(u: Int, edge: Int, step: Int = 0): Unit = {
    color(u) = 1
    var i = 0
    while (i < preceeding(u).length) {
      val v = preceeding(u)(i)
      //res(adjs(u)(i)) = 1 + step % 2
      if (color(v) == 0) dfs0(v, adjs(u)(i), step + 1)
      else if (color(v) == 1) hasCycle = true
      i += 1
    }
    color(u) = 2
    sortedBuilder += u
  }

  for (i <- 0 until n) if (color(i) == 0) dfs0(i, -1)

//  val sorted = sortedBuilder.result
//
//  val order = Array.ofDim[Int](n)
//  for (i <- sorted.indices) {
//    order(sorted(i)) = 1 + i % 2
//  }

  //println(sorted.map(_ + 1).mkString(" "))
  //println(order.mkString(" "))
  //for (i <- 0 until m) res(i) = order(edgeTo(i))

  val visited = Array.fill(n)(false)

  def dfs1(u: Int, u0: Int, step: Int): Unit = {
    /*if (step > 0 && u == u0) hasCycle = true
    else */if (!visited(u)) {
      visited(u) = true
      var i = 0
      while (i < adjs(u).length) {
        val edge = adjs(u)(i)
        res(edge) = step % 2 + 1
        dfs1(edgeTo(edge), u0, step + 1)
        i += 1
      }
    }
  }
//
//  var iter = 0
  for (u <- 0 until n) {
    if (!visited(u)) {
      dfs1(u, u, 0)
    }
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (hasCycle) {
    println(2)
    println(res.mkString(" "))
  } else {
    println(1)
    println(Array.fill(m)(1).mkString(" "))
  }

  Console.flush
}