#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
using PII = pair<ll, ll>;
#define FOR(i,a,n) for(ll i=(ll)a; i<(ll)n; ++i)
#define REP(i,n) FOR(i,0,n)
int dp[2001][2001][2][2];
int main() {
	string S;
	cin >> S;
	int K;
	cin >> K;
	int N = S.size();
	int ans = 0;
	for (int rx = 0; rx < 2; rx++) {
		for (int ry = 0; ry < 2; ry++) {
			fill((int*)dp, (int*)(dp + N + 1), -(1 << 30));
			dp[0][0][rx][ry] = 0;
			for (int i = 0; i < N; i++) {
				for (int j = 0; j <= K; j++) {
					for (int k = 0; k < 2; k++) {
						for (int l = 0; l < 2; l++) {
							if (dp[i][j][k][l] == -(1 << 30)) continue;
							int x = 0, y = 0;
							if (S[i] == 'R')  x = 1;
							else if (S[i] == 'L') x = -1;
							else if (S[i] == 'U') y = 1;
							else y = -1;
							if (k) x *= -1;
							if (l) y *= -1;
							//操作をしない
							dp[i + 1][j][k][l] = max(dp[i + 1][j][k][l], dp[i][j][k][l] + x + y);
							if (j == K) continue;
							//x反転
							dp[i + 1][j + 1][k ^ 1][l] = max(dp[i + 1][j + 1][k ^ 1][l], dp[i][j][k][l] - x + y);
							//y反転
							dp[i + 1][j + 1][k][l ^ 1] = max(dp[i + 1][j + 1][k][l ^ 1], dp[i][j][k][l] + x - y);
						}
					}
				}
			}
			for (int i = 0; i <= K; i++) {
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 2; k++) {
						ans = max(ans, dp[N][i][j][k]);
					}
				}
			}
		}
	}
	cout << ans << endl;

}
