#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define each(a,x) for(auto a : (x))
#define all(a) (a).begin(),(a).end()
#define chmin(a,b) ((a) = min((a),(b)))
#define chmax(a,b) ((a) = max((a),(b)))
#define in_range(x,l,r) ((l)<=(x) && (x)<(r))
#define printvec(a) rep(i,a) cout << a[i] << " \n"[i+1==(a).size()];
#define fs first
#define sc second
#define em emplace
#define eb emplace_back
#define sz size()
#define MP make_pair
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;

const ll INF = 1e8;
const D EPS = 1e-8;
const ll MOD = 1e9+7;

vi solve(const vi &a, int k){
  int n = a.size();
  vector<vi> max_dp(n+1, vi(k+1,-INF)), min_dp(n+1, vi(k+1,+INF));
  max_dp[0][0] = min_dp[0][0] = 0;

  rep(i,n){
    rep(j,k+1){
      int x = (j&1) ? -1 : 1;
      if(max_dp[i][j] != -INF) {
        chmax( max_dp[i+1][j], max_dp[i][j] + a[i]*x);
        chmin( min_dp[i+1][j], max_dp[i][j] + a[i]*x);
        if(j!=k){
          chmax( max_dp[i+1][j+1], max_dp[i][j] + a[i]*x*-1);
          chmin( min_dp[i+1][j+1], max_dp[i][j] + a[i]*x*-1);
        }
      }

      if(min_dp[i][j] != +INF){
        chmax( max_dp[i+1][j], min_dp[i][j] + a[i]*x);
        chmin( min_dp[i+1][j], min_dp[i][j] + a[i]*x);
        if(j!=k){
          chmax( max_dp[i+1][j+1], min_dp[i][j] + a[i]*x*-1);
          chmin( min_dp[i+1][j+1], min_dp[i][j] + a[i]*x*-1);
        }
      }
    }
  }

  vi res(k+1,0);
  rep(i,k+1){
    int max_v = max_dp[n][i] == -INF ? 0 : abs(max_dp[n][i]);
    int min_v = min_dp[n][i] == +INF ? 0 : abs(min_dp[n][i]);
    res[i] = max( max_v, min_v );
  }
  rep(i,k) chmax(res[i+1], res[i]);
  return res;
}

int main(){
  string s; int k;
  cin >> s >> k;

  vi ud, lr;
  rep(i,s.size()){
    if(s[i] == 'U') ud.eb(-1);
    if(s[i] == 'D') ud.eb(+1);
    if(s[i] == 'L') lr.eb(-1);
    if(s[i] == 'R') lr.eb(+1);
  }

  vi ud_k = solve(ud, k), lr_k = solve(lr, k);
  int ans = 0;
  rep(i,k+1){
    chmax(ans, ud_k[i] + lr_k[k-i]);
  }
  cout << ans << endl;
}