/*
	rupc2017day1_E: Graduation_Ceremony
	https://github.com/ixmelrd/rupc2017/blob/master/Graduation_Ceremony/Yazaten/main.cc
*/
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[2][2][2001][2001];

int main()
{
	memset(dp, 0, sizeof(dp));
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	string s; cin >> s;
	int k; cin >> k;
	
	int n = s.length();
	map<char,int> mp[2][2];
	mp[0][0]['U'] = 0, mp[0][0]['D'] = 0, mp[0][0]['R'] =+1, mp[0][0]['L'] =-1;
	mp[0][1]['U'] = 0, mp[0][1]['D'] = 0, mp[0][1]['R'] =-1, mp[0][1]['L'] =+1;
	mp[1][0]['U'] =+1, mp[1][0]['D'] =-1, mp[1][0]['R'] = 0, mp[1][0]['L'] = 0;
	mp[1][1]['U'] =-1, mp[1][1]['D'] =+1, mp[1][1]['R'] = 0, mp[1][1]['L'] = 0;

	rep (x_y, 2){
 		rep (pos_neg, 2){
			for (int i = 1; i <= n; ++i){
				for (int j = 0; j <= k; ++j){
					int sign = (j % 2 == 0 ? +1 : -1);
					int add  = mp[x_y][pos_neg][s[i-1]] * sign;
					dp[x_y][pos_neg][i][j] = dp[x_y][pos_neg][i-1][j] + add;
					if (j - 1 >= 0){
						dp[x_y][pos_neg][i][j] = max(dp[x_y][pos_neg][i][j], dp[x_y][pos_neg][i-1][j-1] - add);
					} // end if
				} // end for
			} // end for
		} // end rep
	} // end rep

	vector<int> x(k+1, 0), y(k+1, 0);
	for (int i = 0; i <= k; ++i){
		x[i] = max(abs(dp[0][0][n][i]), abs(dp[0][1][n][i]));
	} // end for
	rep (i, k){
		x[i+1] = max(x[i+1], x[i]);
	} // end rep
	for (int i = 0; i <= k; ++i){
		y[i] = max(abs(dp[1][0][n][i]), abs(dp[1][1][n][i]));
	} // end for
	rep (i, k){
		y[i+1] = max(y[i+1], y[i]);
	} // end for

	int ans = -1;
	for (int i = 0; i <= k; ++i){
		ans = max(ans, x[i] + y[k-i]);
	} // end for
	cout << ans << endl;

	return 0;
}