#include <bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//#include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define repl(i, a) for (ll i = (ll)0; i < (ll)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
#define fi first
#define se second
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll mod_998244353 = 998244353;
constexpr ll INF = 1LL << 60;

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

//using lll=boost::multiprecision::cpp_int;
//using Double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1024>>;//仮数部が1024桁
template <class T>
inline bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}

template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り

	if (p != -1)
	{
		x %= p;
	}
	T ret = 1;
	while (n > 0)
	{
		if (n & 1)
		{
			if (p != -1)
				ret = (ret * x) % p;
			else
				ret *= x;
		}
		if (p != -1)
			x = (x * x) % p;
		else
			x *= x;
		n >>= 1;
	}
	return ret;
}

using namespace std;
//using namespace atcoder;

void solve()
{
	ll n;
	cin>>n;
	ll ans=0;
	string s="1";
	ll x=stoll(s+s);
	while(x<=n){
		++ans;
		ll num=stoll(s);
		++num;
		s=to_string(num);
		x=stoll(s+s);
	}
	cout<<ans;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for(ll i=0; i<n; i++)
#define Rep(i, j, n) for(ll i=j; i<n; i++)
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmin(T& a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if(a < b) {a = b; return true;} return false;}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

int main() {
    ll n, m; cin >> n >> m;
    vector<ll> h(n);
    rep(i, n) cin >> h[i];
    sort(all(h));
    vector<ll> sum_l(n/2+1), sum_r(n/2+1);
    for(int i=0; i<n-1; i+=2) sum_l[i/2+1] = sum_l[i/2] + h[i+1] - h[i];
    for(int i=0; i<n-1; i+=2) sum_r[i/2+1] = sum_r[i/2] + h[n-i-1] - h[n-i-2];
    ll ans = INF;
    rep(i, m) {
        ll w; cin >> w;
        ll l = -1, r = n-1;
        while(r - l > 1) {
            ll mid = (l + r) / 2;
            if(h[mid] < w) l = mid;
            else r = mid;
        }
        if(r % 2 == 0) chmin(ans, abs(h[r] - w) + sum_l[r/2] + sum_r[(n-r-1)/2]);
        else chmin(ans, abs(h[l] - w) + sum_l[l/2] + sum_r[(n-l-1)/2]);
    }
    cout << ans << endl;
}
