#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

char s[2521];
int n,k;

int dpx[2521][2521][2];
int dpy[2521][2521][2];
int ansx[2521], ansy[2521];

void calc(int dp[2521][2521][2],int ans[2521], string s,int k){
  // dp[i][j][x] := pos i, used j, [min, max]
  int n = s.size();
  REP(i,n+1)REP(j,k+1)dp[i][j][0] = 1e9, dp[i][j][1] = -1e9;
  dp[0][0][0] = dp[0][0][1] = 0;
  REP(i,n)REP(j,k+1){
    if(dp[i][j][0] > dp[i][j][1])continue;
    char c = s[i];
    int way = 1;
    if(c=='R' && j%2==1)way = -1;
    if(c=='L' && j%2==0)way = -1;
    CHMIN(dp[i+1][j][0],dp[i][j][0]+way);
    CHMAX(dp[i+1][j][1],dp[i][j][1]+way);
    // use magic
    CHMIN(dp[i+1][j+1][0],dp[i][j][0]-way);
    CHMAX(dp[i+1][j+1][1],dp[i][j][1]-way);
  }
  // calc ans
  ans[0] = max(abs(dp[n][0][1]),abs(dp[n][0][0]));
  FOR(j,1,k+1){
    ans[j] = ans[j-1];
    if(dp[n][j][0] > dp[n][j][1])continue;
    CHMAX(ans[j], abs(dp[n][j][1]));
    CHMAX(ans[j], abs(dp[n][j][0]));
  }
}

int main(){
  scanf("%s",s);
  scanf("%d",&k);
  n = strlen(s);
  string x,y;
  REP(i,n){
    if(s[i]=='L'||s[i]=='R'){
      x += s[i];
    }else{
      y += s[i]=='U' ? 'L' : 'R';
    }
  }
  calc(dpx,ansx,x,k);
  calc(dpy,ansy,y,k);
  int ans = 0;
  REP(i,k+1)CHMAX(ans, ansx[i]+ansy[k-i]);
  printf("%d\n",ans);
  return 0;
}