#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
#include<algorithm>
constexpr unsigned long long MOD = 998244353;
constexpr unsigned long long IM = 18479187003;
constexpr unsigned long long MOD32 = 665496236;

    char f[5000][5000];
    unsigned long long dp[5000];
int main(){
    int H=0, W=0, K=0;
    char c=getchar_unlocked();
    while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();}
    c=getchar_unlocked();
    while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();}
    c=getchar_unlocked();
    while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();}

    unsigned long long y = H * W - K;
    while(--K>=0){
        int h=0, w=0;
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();}
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();}
        f[h-1][w-1] = getchar_unlocked();
        getchar_unlocked();
    }
    --H; --W;
    dp[0] = 1;
    for(int i = 0; i < H; ++i){
        for (int j = 0; j < W; ++j){
            switch(f[i][j]) {
                case 'R':
                    dp[j + 1] += dp[j];
                    dp[j] = 0;
                    break;
                case '\0':{
        dp[j] *= MOD32;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(dp[j], IM, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
        dp[j] = dp[j] - x * MOD+MOD;
             }
                case 'X':
                    dp[j + 1] += dp[j];
            }
        }
            switch(f[i][W]) {
                case 'R':
                    dp[W] = 0;
                    break;
                case '\0':{
        dp[W] *= MOD32;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(dp[W], IM, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64);
#endif
       dp[W]= dp[W] - x * MOD+MOD;
             }
            }
    }
    for (int j = 0; j < W; ++j){
        switch(f[H][j]) {
            case '\0':{
        dp[j] *= MOD32;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(dp[j], IM, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
        dp[j] = dp[j] - x * MOD+MOD;
               }
            case 'R':
            case 'X':
                dp[j + 1] += dp[j];
        }
    }

    unsigned long long x = 3;
    while(y) {
      if(y & 1) {
        dp[W] *= x;
#ifdef _MSC_VER
        unsigned long long x2;
        _umul128(dp[W], IM, &x2);
#else
        unsigned long long x2 =
            (unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64);
#endif
        dp[W] = dp[W] - x2 * MOD+MOD;
               }
      {
        x *= x;
#ifdef _MSC_VER
        unsigned long long x2;
        _umul128(x, IM, &x2);
#else
        unsigned long long x2 =
            (unsigned long long)(((unsigned __int128)(x)*IM) >> 64);
#endif
        x = x- x2 * MOD + MOD;}
      y >>= 1;
    }
    printf("%d", dp[W] % MOD);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define INF 1020304050607080901

long long steps(long long left, long long t) {
  map<long long,long long> m;
  m[left] = 0;
  queue<long long> q;
  long long td = 1LL<<t;
  q.push(left);
  while (!q.empty()) 
  {
    long long now = q.front();
    q.pop();
    for (long long i=0;i<=t;i++) {
      long long d = 1LL<<i;
      if (now&d) {
        long long to;
        to = now-d;
        if (to >= 0) {
          if (m.find(to) == m.end()) {
            m[to] = m[now]+1;
            q.push(to);
          }
        }
        to = now+d;
        if (to <= td) {
          if (m.find(to) == m.end()) {
            m[to] = m[now]+1;
            q.push(to);
          }
        }
        break;
      } 
    }
  }
  long long ret = INF;
  if (m.find(0) != m.end()) ret = min(ret,m[0]);
  // if (m.find(td) != m.end()) ret = min(ret,m[td]);
  return ret;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  long long x,y;
  cin >> x >> y;
  long long ans = abs(x-y);
  long long beg = x;
  long long t = 0;
  while (true) {
    long long kans = t;
    long long left = abs(beg-y);
    long long d = 1LL<<t;
    kans += left/d;
    kans += steps(left%d,t);
    //cout << beg << ' ' << kans << endl;
    ans = min(ans,kans);
    if (beg > y) break;
    beg *= 2;
    t++;
  }
  /* for (int i=0;i<7;i++) {
    for (int j=0;j<(1<<i);j++) {
      cout << j << ' ' << i << ' ' << steps(j,i) << endl;
    }
  }*/
  cout << ans << endl;
}
