import scala.annotation.tailrec
import scala.collection.mutable
object Main extends App {
  import scala.io.StdIn._
  val forward = """FORWARD (\d+)""".r
  val backward = """BACKWARD (\d+)""".r
  val left = """LEFT""".r
  val right = """RIGHT""".r
  val stop = """STOP""".r
  loop()
  @tailrec def loop(): Unit = {
    val Array(w, h) = readLine.trim.split(' ').map(_.toInt)
    if (w == 0 && h == 0) return
    val area = Area(w, h)
    val state = executeCommand(Continue(Robot()), area)
    println(s"${state.coordinate.x} ${state.coordinate.y}")
    loop()
  }
  @tailrec def executeCommand(robot: WithState[Robot], area: Area): Robot = {
    readLine.trim match {
      case forward(k) ⇒
        executeCommand((0 until k.toInt).foldLeft(robot){case (robot, _) ⇒ robot.tryToContinue(_.forward){r ⇒ area.contains(r.coordinate)}}.toContinue, area)
      case backward(k) ⇒
        executeCommand((0 until k.toInt).foldLeft(robot){case (robot, _) ⇒ robot.tryToContinue(_.backward){r ⇒ area.contains(r.coordinate)}}.toContinue, area)
      case right() ⇒
        executeCommand(robot.map(_.right), area)
      case left() ⇒
        executeCommand(robot.map(_.left), area)
      case stop() ⇒
        robot.fold(identity, identity)
    }
  }

}
sealed trait WithState[+T] {
  def alternative[T2 >: T](alt: ⇒ WithState[T2]): WithState[T2]
  def filterToFail(predicate: T ⇒ Boolean): WithState[T]
  def fold[R, R1 <: R, R2 <: R](forContinue: T ⇒ R1, forStop: T ⇒ R2): R
  def map[T2 >: T](transformer: T ⇒ T2): WithState[T2]
  def tryToContinue[T2 >: T](successor: T ⇒ T2)(predicate: T2 ⇒ Boolean): WithState[T2]
  def toContinue: WithState[T]
}
case class Continue[+T](state: T) extends WithState[T] {
  override def alternative[T2 >: T](alt: ⇒ WithState[T2]): WithState[T2] = this

  override def filterToFail(predicate: T ⇒ Boolean): WithState[T] = if (predicate(state)) this else Stop(state)

  override def fold[R, R1 <: R, R2 <: R](forContinue: T ⇒ R1, forStop: T ⇒ R2): R = forContinue(state)

  override def map[T2 >: T](transformer: T ⇒ T2): WithState[T2] = Continue(transformer(state))

  override def tryToContinue[T2 >: T](successor: T ⇒ T2)(predicate: T2 ⇒ Boolean): WithState[T2] = map(successor).filterToFail(predicate).alternative(Stop(state))

  override def toContinue: WithState[T] = this
}
case class Stop[+T](state: T) extends WithState[T] {
  override def alternative[T2 >: T](alt: ⇒ WithState[T2]): WithState[T2] = alt

  override def filterToFail(predicate: T ⇒ Boolean): WithState[T] = this

  override def fold[R, R1 <: R, R2 <: R](forContinue: T ⇒ R1, forStop: T ⇒ R2): R = forStop(state)

  override def map[T2 >: T](transformer: T ⇒ T2): WithState[T2] = this

  override def tryToContinue[T2 >: T](successor: T ⇒ T2)(predicate: T2 ⇒ Boolean): WithState[T2] = this

  override def toContinue: WithState[T] = Continue(state)
}
case class Robot(coordinate: Coordinate = Coordinate(1, 1), direction: Direction = North) {
  def forward: Robot = Robot(coordinate.move(direction), direction)
  def backward: Robot = Robot(coordinate.move(direction.back), direction)
  def right: Robot = Robot(coordinate, direction.rotateRight)
  def left: Robot = Robot(coordinate, direction.rotateLeft)
}
case class Area(width: Int, height: Int) {
  def contains(coordinate: Coordinate): Boolean = (1 to width).contains(coordinate.x) && (1 to height).contains(coordinate.y)
}
case class Coordinate(x: Int, y: Int) {
  def +(that: Coordinate): Coordinate = Coordinate(x + that.x, y + that.y)
  def move(direction: Direction): Coordinate = this + direction.toRelationalCoordinate
}
sealed trait Direction {
  def toRelationalCoordinate: Coordinate = {
    this match {
      case North ⇒ Coordinate(0, 1)
      case East ⇒ Coordinate(1, 0)
      case South ⇒ Coordinate(0, -1)
      case West ⇒ Coordinate(-1, 0)
    }
  }
  def rotateRight: Direction = {
    this match {
      case North ⇒ East
      case East ⇒ South
      case South ⇒ West
      case West ⇒ North
    }
  }
  def rotateLeft: Direction = {
    this match {
      case North ⇒ West
      case East ⇒ North
      case South ⇒ East
      case West ⇒ South
    }
  }
  def back: Direction = rotateRight.rotateRight
}
object North extends Direction
object East extends Direction
object South extends Direction
object West extends Direction


Measured memory_bytes: 57404