object Main extends App {
  val Array(l, a, b, m) = scala.io.StdIn.readLine.split(" ")
  var str = ""
  var next = BigInt(a)
  var bb = BigInt(b)
  (1 to l.toInt).foreach { _ =>
    str += next.toString
    next += bb
  }

  println(BigInt(str) % m.toInt)
}
