#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


char buf[2001];
int yoko[2001],tate[2001];
int dp_yoko_plus[2000][2001],dp_yoko_minus[2000][2001];
int dp_tate_plus[2000][2001],dp_tate_minus[2000][2001];


int main(){

	int K;
	scanf("%s",buf);
	scanf("%d",&K);

	int yoko_index = 0,tate_index = 0;
	for(int i = 0; buf[i] != '\0'; i++){
		if(buf[i] == 'L' || buf[i] == 'R'){
			if(buf[i] == 'L'){
				yoko[yoko_index++] = -1;
			}else{
				yoko[yoko_index++] = 1;
			}
		}else{
			if(buf[i] == 'D'){
				tate[tate_index++] = -1;
			}else{
				tate[tate_index++] = 1;
			}
		}
	}

	for(int i = 0; i < yoko_index; i++){
		for(int a = 0; a <= K; a++){
			dp_yoko_plus[i][a] = -BIG_NUM;
			dp_yoko_minus[i][a] = BIG_NUM;
		}
	}

	dp_yoko_plus[0][0] = 0;
	dp_yoko_minus[0][0] = 0;

	for(int i = 0; i < yoko_index; i++){
		for(int used = 0; used <= min(i,K); used++){
			if(used%2 == 0){

				if(used < K){
					dp_yoko_plus[i+1][used+1] = max(dp_yoko_plus[i+1][used+1],dp_yoko_plus[i][used]+yoko[i]*(-1));
					dp_yoko_minus[i+1][used+1] = min(dp_yoko_minus[i+1][used+1],dp_yoko_minus[i][used]+yoko[i]*(-1));
				}

				dp_yoko_plus[i+1][used] = max(dp_yoko_plus[i+1][used],dp_yoko_plus[i][used]+yoko[i]);
				dp_yoko_minus[i+1][used] = min(dp_yoko_minus[i+1][used],dp_yoko_minus[i][used]+yoko[i]);

			}else{
				if(used < K){
					dp_yoko_plus[i+1][used+1] = max(dp_yoko_plus[i+1][used+1],dp_yoko_plus[i][used]+yoko[i]);
					dp_yoko_minus[i+1][used+1] = min(dp_yoko_minus[i+1][used+1],dp_yoko_minus[i][used]+yoko[i]);
				}

				dp_yoko_plus[i+1][used] = max(dp_yoko_plus[i+1][used],dp_yoko_plus[i][used]+yoko[i]*(-1));
				dp_yoko_minus[i+1][used] = min(dp_yoko_minus[i+1][used],dp_yoko_minus[i][used]+yoko[i]*(-1));
			}
		}
	}

	for(int i = 0; i < tate_index; i++){
		for(int a = 0; a <= K; a++){
			dp_tate_plus[i][a] = -BIG_NUM;
			dp_tate_minus[i][a] = BIG_NUM;
		}
	}

	dp_tate_plus[0][0] = 0;
	dp_tate_minus[0][0] = 0;

	for(int i = 0; i < tate_index; i++){
		for(int used = 0; used <= min(i,K); used++){
			if(used%2 == 0){

				if(used < K){
					dp_tate_plus[i+1][used+1] = max(dp_tate_plus[i+1][used+1],dp_tate_plus[i][used]+tate[i]*(-1));
					dp_tate_minus[i+1][used+1] = min(dp_tate_minus[i+1][used+1],dp_tate_minus[i][used]+tate[i]*(-1));
				}

				dp_tate_plus[i+1][used] = max(dp_tate_plus[i+1][used],dp_tate_plus[i][used]+tate[i]);
				dp_tate_minus[i+1][used] = min(dp_tate_minus[i+1][used],dp_tate_minus[i][used]+tate[i]);

			}else{

				if(used < K){
					dp_tate_plus[i+1][used+1] = max(dp_tate_plus[i+1][used+1],dp_tate_plus[i][used]+tate[i]);
					dp_tate_minus[i+1][used+1] = min(dp_tate_minus[i+1][used+1],dp_tate_minus[i][used]+tate[i]);
				}

				dp_tate_plus[i+1][used] = max(dp_tate_plus[i+1][used],dp_tate_plus[i][used]+tate[i]*(-1));
				dp_tate_minus[i+1][used] = min(dp_tate_minus[i+1][used],dp_tate_minus[i][used]+tate[i]*(-1));
			}
		}
	}

	int ans = 0;

	for(int a = 0; a <= K; a++){
		for(int b = 0; b <= K-a; b++){

			ans = max(ans,max(dp_yoko_plus[yoko_index][a],abs(dp_yoko_minus[yoko_index][a]))
					+max(dp_tate_plus[tate_index][b],abs(dp_tate_minus[tate_index][b])));
		}
	}

	printf("%d\n",ans);

	return 0;
}



Measured memory_bytes: 34508