object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val s = 'S'::readLine.trim.toList
  val k = readLine.trim.toInt
  val vertical = searchVertical(s.filterNot{List('R', 'L').contains(_)}, k)
  val horizontal = searchHorizontal(s.filterNot{List('U', 'D').contains(_)}, k)
  println(
    (0 to k).map{i ⇒ vertical(i) + horizontal(k - i)}.max
  )
  def searchHorizontal(order: List[Char], k: Int):Array[Int] = {
    val length: Int = order.length
    val memo = Array.tabulate(length + 1){ _ ⇒ Array.tabulate(k + 1){ _ ⇒ Array.tabulate(2){_ ⇒ None: Option[Range]}}}
    def sub(ord: List[Char], position: Int, k: Int, flip: Boolean = false): Range = {
      if (position < 0 || k < 0) Range(0, 0)
      else {
        memo(position)(k)(if (flip) 1 else 0:Int) match {
          case Some(r) ⇒ r
          case None ⇒
            val next = ord match {
              case 'R'::t if k > 0 ⇒ (sub(t, position - 1, k, flip) | sub(t, position - 1, k - 1, !flip)).shift(if (flip) -1 else 1:Int)
              case 'L'::t if k > 0 ⇒ (sub(t, position - 1, k, flip) | sub(t, position - 1, k - 1, !flip)).shift(if (flip) 1 else -1:Int)
              case 'R'::t ⇒ sub(t, position - 1, k, flip).shift(if (flip) -1 else 1:Int)
              case 'L'::t ⇒ sub(t, position - 1, k, flip).shift(if (flip) 1 else -1:Int)
              case _::t ⇒ sub(t, position - 1, k, flip)
              case Nil ⇒ Range(0, 0)
            }
            memo(position)(k)(if (flip) 1 else 0:Int) = Some(next)
            next
        }
      }
    }
    Array.tabulate(k + 1){i ⇒ sub(order, length, i).let{case Range(min, max) ⇒
      //println(s"horizontal $i => Range($min, $max)")
      math.max(math.abs(min), max)}
    }
  }
  def searchVertical(order: List[Char], k: Int):Array[Int] = {
    val length: Int = order.length
    val memo = Array.tabulate(length + 1){ _ ⇒ Array.tabulate(k + 1){ _ ⇒ Array.tabulate(2){_ ⇒ None: Option[Range]}}}
    def sub(ord: List[Char], position: Int, k: Int, flip: Boolean = false): Range = {
      if (position < 0 || k < 0) Range(0, 0)
      else {
        memo(position)(k)(if (flip) 1 else 0:Int) match {
          case Some(r) ⇒ r
          case None ⇒
            val next = ord match {
              case 'U'::t if k > 0 ⇒ (sub(t, position - 1, k, flip) | sub(t, position - 1, k - 1, !flip)).shift(if (flip) -1 else 1:Int)
              case 'D'::t if k > 0 ⇒ (sub(t, position - 1, k, flip) | sub(t, position - 1, k - 1, !flip)).shift(if (flip) 1 else -1:Int)
              case 'U'::t ⇒ sub(t, position - 1, k, flip).shift(if (flip) -1 else 1:Int)
              case 'D'::t ⇒ sub(t, position - 1, k, flip).shift(if (flip) 1 else -1:Int)
              case _::t ⇒ sub(t, position - 1, k, flip)
              case Nil ⇒ Range(0, 0)
            }
            memo(position)(k)(if (flip) 1 else 0:Int) = Some(next)
            next
        }
      }
    }
    Array.tabulate(k + 1){i ⇒ sub(order, length, i).let{case Range(min, max) ⇒
      //println(s"vertical $i => Range($min, $max)")
      math.max(math.abs(min), max)}
    }
  }
  case class Range(min: Int, max: Int) {
    def |(that: Range): Range = Range(math.min(this.min, that.min), math.max(that.max, this.max))
    def shift(diff: Int): Range = Range(min + diff, max + diff)
  }
  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}


Measured memory_bytes: 499444