#include <iostream>
#include <algorithm>
#include <tuple>
using namespace std;

typedef pair<int,int> P; //x,y
P DP[2001][2003][2];//[i?????????][k???][s1????????°?????¶?\?]

P dir(char c, int s1, int s2){
    switch(c){
        case 'U':
            if(s1&1){
                return P(0,-1);
            }else{
                return P(0,1);
            }
        case 'D':
            if(s1&1){
                return P(0,1);
            }else{
                return P(0,-1);
            }
        case 'R':
            if(s2&1){
                return P(-1,0);
            }else{
                return P(1,0);
            }
        case 'L':
            if(s2&1){
                return P(1,0);
            }else{
                return P(-1,0);
            }
    }
    return P(0,0);
}

int main(){
    string S,SS;
    cin>>SS;
    int K;
    cin>>K;
    int ans=-10000;
    for(int Q=0;Q<4;Q++){
        S=SS;
        for(auto &s:S){
            if(Q&1){
                if(s=='U'){
                    s='D';
                }else if(s=='D'){
                    s='U';
                }
            }
            if(Q&2){
                if(s=='R'){
                    s='L';
                }else if(s=='L'){
                    s='R';
                }
            }
        }

        fill(DP[0][0],DP[2000][0],P(-2000,-2000));
        DP[0][0][0]=P(0,0);
        for(int i=0;i<S.size();i++){
            for(int k=0;k<=K;k++){
                P diff, pre, next;
                //????????????
                pre = DP[i][k][1];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],1,k+1);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k][1].first+DP[i+1][k][1].second){
                        DP[i+1][k][1]=next;
                    }
                }
                pre=DP[i][k][0];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],0,k);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k][0].first+DP[i+1][k][0].second){
                        DP[i+1][k][0]=next;
                    }
                }

                //s1
                pre=DP[i][k][1];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],2,k+1);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k+1][0].first+DP[i+1][k+1][0].second){
                        DP[i+1][k+1][0]=next;
                    }
                }
                pre=DP[i][k][0];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],1,k);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k+1][1].first+DP[i+1][k][1].second){
                        DP[i+1][k+1][1]=next;
                    }
                }

                //s2
                pre=DP[i][k][1];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],1,k+2);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k+1][1].first+DP[i+1][k+1][1].second){
                        DP[i+1][k+1][1]=next;
                    }
                }
                pre=DP[i][k][0];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],0,k+1);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k+1][0].first+DP[i+1][k+1][0].second){
                        DP[i+1][k+1][0]=next;
                    }
                }

                //s1+s2
                pre=DP[i][k][1];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],2,k+2);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k+2][0].first+DP[i+1][k+2][0].second){
                        DP[i+1][k+2][0]=next;
                    }
                }
                pre=DP[i][k][0];
                if(pre!=P(-2000,-2000)){
                    diff = dir(S[i],1,k+1);
                    next = P(pre.first+diff.first,pre.second+diff.second);
                    if(next.first+next.second>DP[i+1][k+2][1].first+DP[i+1][k+2][1].second){
                        DP[i+1][k+2][1]=next;
                    }
                }
            }
        }
        for(int k=0;k<=K;k++){
            int x,y;
            tie(x,y)=DP[S.size()][k][0];
            ans=max(ans,x+y);
            tie(x,y)=DP[S.size()][k][1];
            ans=max(ans,x+y);
        }
    }

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