#include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define pb push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T> &st){ os << "["; for(auto a : st) cout << a << ","; os << "]"; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

constexpr int MAX_S = 2010;
constexpr int MAX_K = 2010;

string S;
int K;
int len;
int dp[MAX_S+10][MAX_K+10][2][2];

int do_dp(int U,int R){
    int ret=0;
    rep(i,MAX_S+10) rep(j,MAX_K+10) rep(k,2) rep(l,2) dp[i][j][k][l]=-INF;
    dp[0][0][0][0] = 0;

    for(int i=0;i<len;i++){
        for(int j=0;j<=K;j++){
            for(int k=0;k<=1;k++) for(int l=0;l<=1;l++){
                int u = U;
                int r = R;
                if(k==1) u*=-1;
                if(l==1) r*=-1;

                // ???????????????
                if(S[i]=='U'){
                    dp[i+1][j][k][l] = max(dp[i+1][j][k][l],dp[i][j][k][l]+u);
                }else if(S[i]=='D'){
                    dp[i+1][j][k][l] = max(dp[i+1][j][k][l],dp[i][j][k][l]-u);
                }else if(S[i]=='R'){
                    //cerr << "check : " << i << " " << j << " " << k <<" " << l << endl; 
                    dp[i+1][j][k][l] = max(dp[i+1][j][k][l],dp[i][j][k][l]+r);
                    //cerr << dp[i+1][j][k][l] << endl;
                }else if(S[i]=='L'){
                    dp[i+1][j][k][l] = max(dp[i+1][j][k][l],dp[i][j][k][l]-r);
                }

                // ????????????
                dp[i][j+1][(k+1)%2][l] = max(dp[i][j+1][(k+1)%2][l],dp[i][j][k][l]);
                dp[i][j+1][k][(l+1)%2] = max(dp[i][j+1][k][(l+1)%2],dp[i][j][k][l]);
            }
        }
    }

    for(int i=0;i<=K;i++){
        for(int j=0;j<2;j++) for(int k=0;k<2;k++){
            ret = max(ret,dp[len][i][j][k]);
        }
    }


    // debug
    //rep(i,len) rep(j,K+1) rep(k,2) rep(l,2){
    //    cerr << dp[i][j][k][l] << endl;
    //} 

    return ret;
}

int main(){
    int ans = 0;
    cin >> S >> K;
    len = S.size();

    ans = max(ans,do_dp(1,1));
    ans = max(ans,do_dp(1,-1));
    ans = max(ans,do_dp(-1,1));
    ans = max(ans,do_dp(-1,-1));

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 66892