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

typedef long long ll;
typedef vector<long long> vl;
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define ln "\n"
#define forn(i,e) for(ll i=0; i<e; i++)
#define forsn(i,s,e) for(ll i=s; i<e; i++)
#define rforn(i,e) for(ll i=e; i>=0; i--)
#define rforsn(i,s,e) for(ll i=s; i>=e; i--)
#define vasort(v) sort(v.begin(), v.end())
#define vdsort(v) sort(v.begin(), v.end(),greater<int>())
#define F first
#define S second

#define out1(x1) cout << x1 << ln
#define out2(x1,x2) cout << x1 << " " << x2 << ln
#define out3(x1,x2,x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1,x2,x3,x4) cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define out5(x1,x2,x3,x4,x5) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << ln
#define out6(x1,x2,x3,x4,x5,x6) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << ln

#define in1(x1) cin >> x1
#define in2(x1,x2) cin >> x1 >> x2
#define in3(x1,x2,x3) cin >> x1 >> x2 >> x3
#define in4(x1,x2,x3,x4) cin >> x1 >> x2 >> x3 >> x4
#define in5(x1,x2,x3,x4,x5) cin >> x1 >> x2 >> x3 >> x4 >> x5
#define in6(x1,x2,x3,x4,x5,x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6

#define arrin(a,n) forn(i,n) cin >> a[i];
#define arrout(a,n) forn(i,n) {cout << a[i] << " ";} cout << ln;

#define zoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

const ll PI = acosl(-1.0);
ll mod=1e9+7;
//ll mod=998244353;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

/*
const ll MAXN=1e7+1;

//ll spf[MAXN];
//vector<int> prime;

void sieve()
{
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)

        // marking smallest prime factor for every
        // number to be itself.
        spf[i] = i;

    // separately marking spf for every even
    // number as 2
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++)
    {
        // checking if i is prime
        if (spf[i] == i)
        {
            prime.push_back(i);
            // marking SPF for all numbers divisible by i
            for (ll j=i*i; j<MAXN; j+=i)

                // marking spf[j] if it is not
                // previously marked
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
//ll fact[100001],infac[100001];*/

ll fe(ll a,ll b){
    ll temp=a;
    ll ans=1;
    while(b){
        if(b&1){
            ans*=temp;
            ans%=mod;
        }
        b/=2;
        temp=temp*temp;
        temp%=mod;
    }
    return ans;
}

/*ll mi(ll a){
    return(fe(a,mod-2));
}

void fac(){
    fact[0]=1;
    infac[0]=1;
    forsn(i,1,101){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
        infac[i]=mi(fact[i]);
        infac[i]%=mod;
    }
}

ll ncr(ll a,ll b){
    ll ans=fact[a];
    ans*=infac[b];
    ans%=mod;
    ans*=infac[a-b];
    ans%=mod;
    return ans;
}
///////////////////////////////////
int is_prime(int i){
    int t=sqrt(i);
    forsn(j,2,t+1)if(i%j==0)return 0;
    return 1;
}

vector<int> prim;
void pri(){
    forsn(i,2,1001){
        if(is_prime(i))prim.push_back(i);
    }
}

//////////////////////////////////////////*/
/*int n,m;
vector<string> gr;
vector<vector<int>> don;
int xx[2]={1,0},yy[2]={0,1};
map<pair<int,int>,vector<pair<int,int>>> mp;

void dfs(int x,int y){
    don[x][y]=1;
    forn(i,2){
        int nx=x+xx[i],ny=y+yy[i];
        if(nx>=0 && nx<n && ny>=0 && ny<m && gr[nx][ny]!='#'){
            mp[{nx,ny}].push_back({x,y});
            if(don[nx][ny]==0)dfs(nx,ny);
        }
    }
}

map<int,int> co;

void dfs1(int x,int y){
    don[x][y]=1;
    co[x+y]++;
    for(auto j:mp[{x,y}]){
        if(don[j.F][j.S]==0){
            dfs1(j.F,j.S);
        }
    }
}*/

void solve(){
    int n,x;
    in2(n,x);
    vector<pair<int,int>> a;
    int l,r;
    forn(i,n){
        in2(l,r);
        a.push_back({l,r});
    }
    sort(a.begin(),a.end());
    int ans=0;
    int r1=0;
    forn(i,n){
        if(a[i].S<=r1)continue;
        else if(a[i].F>=r1 && i!=0){
            ans+=(a[i].F-r1)%x;
            ans+=(a[i].S-a[i].F+1);
            r1=a[i].S;
            //out1(ans);
        }
        else if(a[i].F>=r1){
            ans+=(a[i].S-a[i].F+1);
            r1=a[i].S;
            ///out1(ans);
        }

        else{
            ans+=a[i].S-r1;
            r1=a[i].S;
            //out1(ans);
        }
    }
    out1(ans);
}

int main(){

    zoom;

    int t=1;
    //in1(t);
    forn(i,t){
        solve();
    }
}


#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n, x, t = 0;
    cin >> n >> x;
    int l[n], r[n], s = 1;
    for(int i = 0; i < n; i++) {
        cin >> l[i] >> r[i];
        // greedy: skip as close to the next
        // starting minute without going over
        while (s + x <= l[i]) {
            s += x;
        }
        // plus one for bounds ex) [4,6] is 3 minutes
        t += r[i] - s + 1;
        s = r[i] + 1;
    }
    cout << t;

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n, x, t = 0;
    cin >> n >> x;
    // skip to the nearest minute or starting minute of
    // the next best part without going over
    for(int start, end, prev_end, i = 0; i < n; i++) {
        cin >> start >> end;
        if (i == 0) {
            start = ((start - 1) / x) * x;
            t += end - start;
            prev_end = end;
        }
        else {
            start = ((start - prev_end) / x) * x + prev_end;
            t += end - start;
            prev_end = end;
        }
    }
    cout << t;

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n, x, t = 0;
    cin >> n >> x;
    int l[n], r[n], s = 1;
    // greedy
    for(int i = 0; i < n; i++) {
        cin >> l[i] >> r[i];
        while (s + x <= l[i]) {
            s += x;
        }
        // plus one for bounds ex) [4,6] is 3 minutes
        t += r[i] - s + 1;
        s = r[i] + 1;
    }
    cout << t;

    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int n, x;
    cin>>n>>x;
    int l[n],r[n];
    for( int i =0 ; i<n; i++)
    {
        cin>>l[i];
        cin>>r[i];
    }
    int ans =0;

    if(l[0] != 1)
        ans = (l[0] % x) -1;
    //cout<<ans;
    for( int i =0 ; i<n ;i++)
    {
        ans += r[i] + 1 - l[i];
        //cout<<ans<<endl;
        if(i+1<n)
        {
            ans += (l[i+1] - (r[i]+1))%x;

        }
    }

    cout<<ans<<endl;
}


#include<iostream>
using namespace std;

int main()
{
    int n, x;
    cin>>n>>x;
    int l[n],r[n];
    for( int i =0 ; i<n; i++)
    {
        cin>>l[i];
        cin>>r[i];
    }
    int ans =0, time =1;
    for( int i =0 ; i<n; i++)
    {
        time += x*((l[i] - time) / x);
        ans += r[i] - time + 1;
        time = r[i] + 1;
    }

        cout<<ans<<endl;
}


#include<iostream>
using namespace std;

int main()
{
    int n, x;
    cin>>n>>x;
    int l[n],r[n];
    for( int i =0 ; i<n; i++)
    {
        cin>>l[i];
        cin>>r[i];
    }
    int ans =0;

    if(l[0] != 1)
        ans = l[0] % x -1;
    //cout<<ans;
    for( int i =0 ; i<n ;i++)
    {
        ans += r[i] + 1 - l[i];
        //cout<<ans<<endl;
        if(i+1<n)
        {
            ans += (l[i+1] - (r[i]+1))%x;

        }
    }

    cout<<ans<<endl;
}


#include<iostream>
using namespace std;

int main()
{
    int n, x;
    cin>>n>>x;
    int l[n],r[n];
    for( int i =0 ; i<n; i++)
    {
        cin>>l[i];
        cin>>r[i];
    }

    int ans = l[0] % x;
    for( int i =0 ; i<n ;i++)
    {
        ans += r[i] - l[i];
        if(i+1<n)
            ans += (l[i+1] - r[i])%x;
    }
    if(l[0] == 1)
        ans += 1;
    cout<<ans<<endl;
}


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

int main()
{
    ll n, a, ans = 0;

    cin >> n >> a;

    for(ll i = 0; i < n; i++){
        int x, y, r;

        cin >> x >> y;

        r = ((x / a) * a) + 1;

        if(r > x) r -= a;

        ans += (y - r + 1);
    }

    cout << ans << endl;

    return 0;
}


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

int main()
{
    ll n, a, ans = 0, r = 1;

    cin >> n >> a;

    for(ll i = 0; i < n; i++){
        ll x, y;

        cin >> x >> y;

        ans += (x - r) % a;
        r = y + 1;
        ans += (y - x + 1);
    }

    cout << ans << endl;

    return 0;
}


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

int main()
{
    ll n, a, ans = 0;

    cin >> n >> a;

    for(ll i = 0; i < n; i++){
        int x, y, r;

        cin >> x >> y;

        r = ((x / a) * a);

        if(r == x) r -= a;

        ans += (y - r);
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector < int >
#define vll vector < ll >
#define vs vector < string >
#define sc scanf
#define pf printf
#define pii pair < int, int >
#define pll pair < ll, ll >
#define pci pair < char, int >
#define pb push_back
#define maxn 100005
#define inf 1e9
#define pi acos(-1)
#define fast ios_base::sync_with_stdio(false)
#define fffc cin.tie(NULL)
using namespace std;

int main()
{
    fast;
    fffc;

    int n, x, fi, se;

    cin >> n >> x;

    pii a[n + 5];

    for(int i = 0; i < n; i++) cin >> fi >> se, a[i] = {fi, se};

    int sum = 0, i = 0, tmp = 1;

    while(1){
        if(i == n) break;

        if(tmp + x > a[i].first){
            sum += (a[i].second - tmp + 1);
            tmp = a[i].second + 1;
            i++;
        }
        else tmp += x;
    }

    cout << sum << endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        if((j+x)<=a)
        {
            while(j<=a)
                j+=x;
            j=j-x;
            int m = (b-j)+1;
            count+=m;
            j=b+1;
        }
        else
        {
            if(j==a)
                count+=((b-a)+1);
            else{
            int res = (j+b)-1;
            count+=res;}
            j=b+1;
        }

    }
    cout<<count;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        if((j+3)<=a)
        {
            while(j<=a)
                j+=x;
            j=j-x;
            int m = (b-j)+1;
            count+=m;
            j=b+1;
        }
        else
        {
            if(j==a)
                count+=((b-a)+1);
            else{
            int res = (j+b)-1;
            count+=res;}
            j=b+1;
        }

    }
    cout<<count;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        while((j+x)<=a)
            j+=x;
        count+=((b-j)+1);
        j=b+1;
    }
    cout<<count;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        if((j+3)<=a)
        {
            while(j<=a)
                j+=x;
            j=j-x;
            int m = (b-j)+1;
            count+=m;
            j=b+1;
        }
        else
        {
            if(j==a)
                count++;
            else{
            int res = (j+b)-1;
            count+=res;}
            j=b+1;
        }

    }
    cout<<count;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        if((j+3)<=a)
        {
            while(j<=a)
                j+=x;
            j=j-x;
            int m = (b-j)+1;
            count+=m;
            j=b+1;
        }
        else
        {
            int res = (j+b)-1;
            count+=res;
            j=b+1;
        }

    }
    cout<<count;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        if((j+x)<=a)
        {
            while(j<=a)
                j+=x;
            if(j>a)
                j=j-x;
            int m = (b-j)+1;
            count+=m;
            j=b+1;
        }
        else
        {
            if(j==a)
                count+=((b-a)+1);
            else{
            int res = (j+b)-1;
            count+=res;}
            j=b+1;
        }

    }
    cout<<count;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x,j=1,count=0;
    cin>>n>>x;
    int a,b;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b;
        if((j+3)<=a)
        {
            while(j<=a)
                j+=x;
            if(j>a)
                j=j-x;
            int m = (b-j)+1;
            count+=m;
            j=b+1;
        }
        else
        {
            if(j==a)
                count+=((b-a)+1);
            else{
            int res = (j+b)-1;
            count+=res;}
            j=b+1;
        }

    }
    cout<<count;
    return 0;
}


#include <bits/stdc++.h>
#include <string>
//#include <boost/multiprecision/cpp_int.hpp>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll  long long int
#define fp cout<<fixed<<setprecision(9);
#define ifor(j,n) for(int i=j ;i<n;i++)
#define jfor(k,n) for(ll j=k ;j<n;j++)
#define pb push_back
#define mod 1000000007
using namespace std;
//using namespace boost::multiprecision;

//Compiler version g++ 6.3.0
int search (vector <long long> v,long long m,int n)
{

    int start = 0;
    int ans = -1;
    while (start <=n)
    {
     int mid = (start + n) / 2;
     if (v[mid] <= m)
         start = mid + 1;
     else
   {
   ans = mid;
   n = mid - 1;

       }

    }
    return ans;
}

int main()
{
 FAST
   int n,x;
   cin>>n>>x;
   int a[n+1],b[n+1];
   a[0]=0;b[0]=0;
   ifor(1,n+1)
    cin>>a[i]>>b[i];
    int tim=0;
    ifor(1,n+1)
    {
        tim+=(a[i]-b[i-1])%x;

        tim+=(b[i]-a[i]+1);
        b[i]++;

    }
    cout<<tim;

}


#include <bits/stdc++.h>
#include <string>
//#include <boost/multiprecision/cpp_int.hpp>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll  long long int
#define fp cout<<fixed<<setprecision(9);
#define ifor(j,n) for(int i=j ;i<n;i++)
#define jfor(k,n) for(ll j=k ;j<n;j++)
#define pb push_back
#define mod 1000000007
using namespace std;
//using namespace boost::multiprecision;

//Compiler version g++ 6.3.0
int search (vector <long long> v,long long m,int n)
{

    int start = 0;
    int ans = -1;
    while (start <=n)
    {
     int mid = (start + n) / 2;
     if (v[mid] <= m)
         start = mid + 1;
     else
   {
   ans = mid;
   n = mid - 1;

       }

    }
    return ans;
}

int main()
{
 FAST
   int n,x;
   cin>>n>>x;
   int t[n][2];
   ifor(0,n)
    cin>>t[i][0]>>t[i][1];
    int tim=0,sec=1;
    ifor(0,n)
    {
        if(sec+x<=t[i][0])
        {
            sec+=(((t[i][0]-sec)/x)*x);
        }

        tim+=(t[i][1]-t[i][0]+1)+(t[i][0]-sec);

    }
    cout<<tim;

}


#include <bits/stdc++.h>
#include <string>
//#include <boost/multiprecision/cpp_int.hpp>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll  long long int
#define fp cout<<fixed<<setprecision(9);
#define ifor(j,n) for(int i=j ;i<n;i++)
#define jfor(k,n) for(ll j=k ;j<n;j++)
#define pb push_back
#define mod 1000000007
using namespace std;
//using namespace boost::multiprecision;

//Compiler version g++ 6.3.0
int search (vector <long long> v,long long m,int n)
{

    int start = 0;
    int ans = -1;
    while (start <=n)
    {
     int mid = (start + n) / 2;
     if (v[mid] <= m)
         start = mid + 1;
     else
   {
   ans = mid;
   n = mid - 1;

       }

    }
    return ans;
}

int main()
{
 FAST
   int n,x;
   cin>>n>>x;
   int a[n+1],b[n+1];
   a[0]=0;b[0]=0;
   ifor(1,n+1)
    cin>>a[i]>>b[i];
    int tim=0;
    ifor(1,n+1)
    {
        tim+=(b[i]-b[i-1])%x;

        tim+=(b[i]-a[i]+1);
        b[i]++;

    }
    cout<<tim;

}


#include <bits/stdc++.h>
#include <string>
//#include <boost/multiprecision/cpp_int.hpp>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll  long long int
#define fp cout<<fixed<<setprecision(9);
#define ifor(j,n) for(int i=j ;i<n;i++)
#define jfor(k,n) for(ll j=k ;j<n;j++)
#define pb push_back
#define mod 1000000007
using namespace std;
//using namespace boost::multiprecision;

//Compiler version g++ 6.3.0
int search (vector <long long> v,long long m,int n)
{

    int start = 0;
    int ans = -1;
    while (start <=n)
    {
     int mid = (start + n) / 2;
     if (v[mid] <= m)
         start = mid + 1;
     else
   {
   ans = mid;
   n = mid - 1;

       }

    }
    return ans;
}

int main()
{
 FAST
   int n,x;
   cin>>n>>x;
   int a[n+1],b[n+1];
   a[0]=0;b[0]=1;
   ifor(1,n+1)
    cin>>a[i]>>b[i];
    int tim=0;
    ifor(1,n+1)
    {
        tim+=(a[i]-b[i-1])%x;

        tim+=(b[i]-a[i]+1);
        b[i]++;

    }
    cout<<tim;

}


        #include <bits/stdc++.h>
        #define endl '\n'
        using namespace std;
        #define int unsigned long long
        int bintodec(int a){
        int counter=0;
        while(a!=0){
        if(a%2!=0)
        counter++;
        a=a/2;
        }
        return counter;
        }
        int_fast32_t main()
        {
            cin.tie(0);
            cout.tie(0);
            iostream::sync_with_stdio(0);
            int best,skip,L,R,counter=0;
            cin>>best>>skip;
            for(int i=0;i<best;i++){
            cin>>L>>R;
            counter=counter+(R-((L==1)?0:L))+L%skip;
            }
            cout<<counter;
            return 0;
    }


        #include <bits/stdc++.h>
        #define endl '\n'
        using namespace std;
        #define int unsigned long long
        int bintodec(int a){
        int counter=0;
        while(a!=0){
        if(a%2!=0)
        counter++;
        a=a/2;
        }
        return counter;
        }
        int_fast32_t main()
        {
            cin.tie(0);
            cout.tie(0);
            iostream::sync_with_stdio(0);
            int best,skip,L,R,counter=0;
            cin>>best>>skip;
            int prev=0;
            for(int i=0;i<best;i++){
            cin>>L>>R;
            counter=counter+(R-L)+L%(skip+prev);
            //cout<<counter<<endl;
            prev=R+1;
            }
            cout<<counter+1;
            return 0;
    }


        #include <bits/stdc++.h>
        #define endl '\n'
        using namespace std;
        #define int unsigned long long
        int bintodec(int a){
        int counter=0;
        while(a!=0){
        if(a%2!=0)
        counter++;
        a=a/2;
        }
        return counter;
        }
        int_fast32_t main()
        {
            cin.tie(0);
            cout.tie(0);
            iostream::sync_with_stdio(0);
            int best,skip,L,R,counter=0;
            cin>>best>>skip;
            for(int i=0;i<best;i++){
            cin>>L>>R;
            counter=counter+(R-L)+L%skip;
            }
            cout<<counter-(best-2);
            return 0;
    }


        #include <bits/stdc++.h>
        #define endl '\n'
        using namespace std;
        #define int long long
        int bintodec(int a){
        int counter=0;
        while(a!=0){
        if(a%2!=0)
        counter++;
        a=a/2;
        }
        return counter;
        }
        int_fast32_t main()
        {
            cin.tie(0);
            cout.tie(0);
            iostream::sync_with_stdio(0);
            int best,skip,L,R,counter=0,maxer=0;
            cin>>best>>skip;
            int prev=1;
            for(int i=0;i<best;i++){
            cin>>L>>R;
            maxer=max(R,maxer);
            while(prev+skip<=L){
            counter=counter-skip;
            prev=prev+skip;
            }
            //cout<<counter<<endl;
            prev=R+1;
            }
            counter=counter+maxer;
            cout<<counter;
            return 0;
    }


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	ll n,x,arr[50][2],sum=0;
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];
        		if(arr[i][0]%x==0) sum+=arr[i][0]%x+1;
        		else sum+=arr[i][0]%x;
        		sum+=arr[i][1]-arr[i][0];}
        		cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	ll n,x,arr[50][2],sum=0,k=0;
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];
        		}
        		REP(i,0,n-1){
        			sum+=(arr[i][0]-k-1)%x;
        			sum+=arr[i][1]-arr[i][0]+1;
        			k=arr[i][1];
        		}
        		cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	long long int n,x,sum=0,k,arr[55][2];
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];}
        	if(n==1){
        		sum=sum+arr[0][1];
        		REP(i,1,n-1){
        			k=arr[i][0]/x;

        		sum+=arr[i][1]-k*x;
        		}
        	}
        	else{
        	REP(i,0,n-1){

        		if(arr[i+1][0]>arr[i][1]){
        		k=arr[i][0]/x;}

        		sum+=arr[i][1]-k*x;
       }}
       cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	long long int n,x,sum=0,k,arr[55][2];
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];}
        	if(n==1){
        		sum=sum+arr[0][1];
        		REP(i,1,n-1){
        			k=arr[i][0]/x;

        		sum+=arr[i][1]-k*x;
        		}
        	}
        	else{
        	REP(i,0,n-1){

        		if(arr[i+1][0]>arr[i][1]&&i+1<=n){
        		k=arr[i][0]/x;}

        		sum+=arr[i][1]-k*x;
       }}
       cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	ll n,x,arr[50][2],sum=0;
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];
        		if(arr[i][0]%x==0) sum+=arr[i][0]%x+1;
        		else sum+=arr[i][0]%x;
        		if(arr[i][1]%x!=0) sum+=x-arr[i][1]%x;
        		sum+=arr[i][1]-arr[i][0];}
        		cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	ll n,x,arr[50][2],sum=0,k=0;
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];
        		}
        		REP(i,0,n-1){
        			sum+=(arr[i][0]-k+1)%x;
        			sum+=arr[i][1]-arr[i][0]+1;
        			k=arr[i][1];
        		}
        		cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	long long int n,x,sum=0,k,arr[55][2];
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];}
        	if(n==1){
        		sum=sum+arr[0][1];
        		REP(i,1,n-1){
        			k=arr[i][0]/x;

        		sum+=arr[i][1]-k*x;
        		}
        	}
        	else{
        	REP(i,0,n-1){

        		k=arr[i][0]/x;

        		sum+=arr[i][1]-k*x;
       }}
       cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007

int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int t;
    t=1;
     //  cin>>t;
    while(t--){
        	ll n,x,arr[50][2],sum=0;
        	cin>>n>>x;
        	REP(i,0,n-1){cin>>arr[i][0]>>arr[i][1];
        		if(arr[i][0]%x==0) sum+=arr[i][0]%x+1;
        		else sum+=arr[i][0]%x;
        		if(arr[i][1]%x!=0) sum+=arr[i][1]%x;
        		sum+=arr[i][1]-arr[i][0];}
        		cout<<sum;

             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
int main(){
	int a,b,k=1,ans=0;
	int m,n;
	scanf("%d%d",&a,&b);
	for(int i=1;i<=a;i++)
	{
		scanf("%d%d",&m,&n);
		ans+=(m-k)%b;
		ans+=n-m+1;
		k=n+1;
	}
	cout<<ans<<endl;
}


#include <cstdio>

int main() {
    int n, x;
    scanf("%d%d", &n, &x);
    int cur = 1, ans = 0;
    while (n--) {
        int l, r;
        scanf("%d%d", &l, &r);
        ans += (l - cur) % x + r - l + 1;
        cur = r + 1;
    }
    printf("%d\n", ans);
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,xx,a,b;
    cin>>n>>xx;
    int begin=1;
    int sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b;
        while(a-begin>=xx){begin+=xx;} //没有跳过就继续
        sum+=b-begin+1;  //不能再跳了，就直接看到b处
        begin=b+1;
    }
    cout<<sum<<endl;
    return 0;
}


#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int n,x,l,r;
  scanf("%d %d",&n,&x);
  int e = 1,t=0;
  for (int i = 0; i < n; i++){
    scanf("%d %d",&l,&r);
    e += ((l-e)/x * x);
    if (e != l){
      int f = abs(e-l);
      e += f;
      t += f;
    }

    e+= abs(r-l+1);
    t+= abs(r-l+1);
  }

  printf("%d",t);

}


#include <iostream>
#include <math.h>
#define ll long long

using namespace std;

int main() {

  int n,x;
  ll t=1,m=1,l,r;
  scanf("%d %d",&n,&x);
  while(n--){

    scanf("%lld %lld",&l,&r);

    double f = floor((l - t)/x);
    t += f*x;
    if (t < l){

      int d = abs(l-t);
      t += d;
      m += d;
    }

    m += (r-l);
    t += (r-l);
  }

  cout << m << endl;

}


#include <iostream>

#define ll long long

using namespace std;

int main() {

  int n,x;
  ll t=1,m=1,l,r;
  scanf("%d %d",&n,&x);
  while(n--){

    scanf("%lld %lld",&l,&r);

    int f = (l - t)/x;
    t += f*x;
    if (t < l){

      int d = abs(l-t);
      t += d;
      m += d;
    }

    m += (r-l);
    t += (r-l);
  }

  cout << m << endl;

}


#include <iostream>

using namespace std;

int main() {

  int n,x,t = 1,l,r,m = 1;
  scanf("%d %d",&n,&x);
  while(n--){

    scanf("%d %d",&l,&r);

    int f = (l - t)/x;
    t += f*x;
    if (t != l){

      int d = abs(l-t);
      t += d;
      m += d;
    }

    m += (r-l);
    t += (r-l);
  }

  cout << m << endl;

}


#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int n,x,l,r;
  scanf("%d %d",&n,&x);
  int e = 1,t=1;
  for (int i = 0; i < n; i++){

    scanf("%d %d",&l,&r);
    e += ((l-e)/x * x);
    if (e != l){
      int f = abs(e-l);
      e += f;
      t += f;
    }

    e+= abs(r-l);
    t+= abs(r-l);
  }

  printf("%d",t);

}


#include <iostream>

using namespace std;

int main() {

  int n,x,t = 1,l,r,m = 1;
  scanf("%d %d",&n,&x);
  while(n--){

    scanf("%d %d",&l,&r);

    int f = (l - t)/x;
    t += f*x;
    if (t < l){

      int d = abs(l-t);
      t += d;
      m += d;
    }

    m += (r-l);
    t += (r-l);
  }

  cout << m << endl;

}


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

int main() {
	int n,x,i,l,r,t=0,y=0;
	cin >> n >> x;
	for (i = 0; i < n; i++){
		cin >> l >> r;
		t += (l-1-y) % 3;
		t += r-(l-1);
		y = r;
	}
	cout << t << endl;
	return 0;
}


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

int main() {
	int n,x,i,l,r,t=0,y=0;
	cin >> n >> x;
	for (i = 0; i < n; i++){
		cin >> l >> r;
		t += (l-1-y) % x;
		t += r-(l-1);
		y = r;
	}
	cout << t << endl;
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main()
{
	long int n, x;
	cin >> n >> x;
	vector< long int[2]> vect(n);
	int min=0;
	for (int i = 0; i < n; i++)
	{
		cin >> vect[i][0] >> vect[i][1];
		vect[i][1] += 1;
		min = min + vect[i][1] - vect[i][0];
	}
	 min = min+(vect[0][0] - 1) % x;
	for (int i = 0; i < n - 1; i++)
	{
		min = min + ((vect[i + 1][0] - vect[i][1]) % x);
	}
	cout << min;
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <sstream>
#define ll long long
#define E "\n\n"
#define S "    "
#define GNB 0
using namespace std;
int main() {
	ll n, x, r1, r2,sum1=0,sum2 = 0;
	cin >> n >> x;
	while(n--)
	{
		cin >> r1>> r2;
		while (sum2+x < r1 )sum2 += x;
		sum1 += r2 - sum2;
	}
	cout << sum1;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <sstream>
#define ll long long
#define E "\n\n"
#define S "    "
#define GNB 0
using namespace std;
int main() {
	int n, x, r1, r2,sum1=0;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
		int sum2=0;
		cin >> r1>> r2;
		while (sum2+x < r1 )sum2 += x;
		sum1 += r2 - sum2;
	}
	cout << sum1;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <sstream>
#define ll long long
#define E "\n\n"
#define S "    "
#define GNB 0
using namespace std;
int main() {
	ll n, x, r1, r2,sum1=0,sum2 = 0;
	cin >> n >> x;
	for (ll i = 0; i < n; i++)
	{
		cin >> r1>> r2;
		while (sum2+x <= r1 )sum2 += x;
		sum1 += r2 - sum2;
	}
	cout << sum1;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <sstream>
#define ll long long
#define E "\n\n"
#define S "    "
#define GNB 0
using namespace std;
int main() {
	ll n, x, r1, r2,sum1=0,sum2 = 1;
	cin >> n >> x;
	for (ll i = 0; i < n; i++)
	{
		cin >> r1>> r2;
		while (sum2+x <= r1 )sum2 += x;
		sum1 += r2 - sum2+1;
	}
	cout << sum1;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <sstream>
#define ll long long
#define E "\n\n"
#define S "    "
#define GNB 0
using namespace std;
int main() {
	ll n, x, r1, r2,sum1=0,sum2 = 0;
	cin >> n >> x;
	for (ll i = 0; i < n; i++)
	{
		cin >> r1>> r2;
		while (sum2+x < r1 )sum2 += x;
		sum1 += r2 - sum2;
	}
	cout << sum1;
}


#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <sstream>
#define ll long long
#define E "\n\n"
#define S "    "
#define GNB 0
using namespace std;
int main() {
	ll n, x, r1, r2,sum1=0,sum2 = 1;
	cin >> n >> x;
	for (ll i = 0; i < n; i++)
	{
		cin >> r1>> r2;
		while (sum2+x <= r1 )sum2 += x;
		sum1 += r2 - sum2+1;
		sum2 = r2+1;
	}
	cout << sum1;
}


#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{ int n;
  long unsigned int x,s[55][2],sum=0,t=0;
  cin>>n>>x;
  for(int i=0;i<n;i++)
  for(int j=0;j<2;j++)
  {
      cin>>s[i][j];
  }
    int i=0;
    while(i<n)
    {
       while(sum+x<s[i][0])
        {sum +=x;}
        while(sum<s[i][0])
        {sum++;
        t++;}
       sum += s[i][1]-s[i][0];
       t += s[i][1]-s[i][0];

       i++;

    }
    cout<<t;
}


#include <iostream>

using namespace std;

int main()
{
    int n,x;
    cin>>n>>x;
    int l,r, p=1;long sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        p+=x*((l-p)/x);
        sum+=r-p+1;
        p=r+1;
    }

    cout<<sum;
    return 0;
}


//AshhsA//
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void solve()
{
	int n, x, l, r, pos = 1, counter = 0;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
			cin >> l >> r;
			if (l - pos >= x)
			{
				while (l - pos >= x)
				{
					pos += x;
				}
				counter += r - pos + 1;
				pos = r + 1;
			}
			else
			{
				counter += r - pos + 1;
				pos = r + 1;
			}
	}
	cout << counter;
}
int main()
{
	solve();
}


//AshhsA//
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void solve()
{
	int n, x, l, r, pos = 1, counter = 0;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
			cin >> l >> r;
			if (l - pos >= x)
			{
				pos += x;
				counter += r - pos + 1;
				pos += counter;
			}
			else
			{
				pos = l - pos + 1;
				counter += r - l + pos;
				pos = counter + 1;
			}
	}
	cout << counter;
}
int main()
{
	solve();
}


//AshhsA//
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void solve()
{
	int n, x, l, r, pos = 1, counter = 0;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
			cin >> l >> r;
			if (l - pos >= x)
			{
				while (l - pos >= x)
				{
					pos += x;
					counter += r - pos + 1;
					pos += counter;
				}
			}
			else
			{
				pos = l - pos + 1;
				counter += r - l + pos;
				pos = counter + 1;
			}
	}
	cout << counter;
}
int main()
{
	solve();
}


//AshhsA//
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void solve()
{
	int n, x, l, r, pos = 1, counter = 0;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
		cin >> l >> r;
		while (l - pos >= x)
			pos += x;
		counter += r - pos + 1;
	}
	cout << counter;
}
int main()
{
	solve();
}


//AshhsA//
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
void solve()
{
	int n, x, l, r, pos = 1, counter = 0;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
			cin >> l >> r;
			if (l - pos >= x)
			{
				while (l - pos >= x)
				{
					pos += x;
				}
				counter += r - pos + 1;
				pos += counter;
			}
			else
			{
				pos = l - pos + 1;
				counter += r - l + pos;
				pos = counter + 1;
			}
	}
	cout << counter;
}
int main()
{
	solve();
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <vector>
using namespace std;
int n,x;
int main(){
    cin>>n>>x;
    int cur=1;//从第一分钟开始处理
    //cur表示当前在处理第几分钟
    int ans=0;
    while (n--) {
        int l,r;
        cin>>l>>r;
        while (cur+x<=l) {
            cur+=x;
        }
        ans+=r-cur+1;
        cur=r+1;
    }
    return 0;
}


#include<iostream>
using namespace std;
int N,X;
int L,R;
int now;
int cnt;
int main()
{
cin>>N>>X;
now=1;
cnt=0;
for(int i=0;i<N;i++)
    {cin>>L>>R;
    while(now+X<=L)//可以按
        now=now+X;
    cnt=cnt+R-now+1;
    now=R+1;
    }
cout<<cnt<<endl;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <vector>
using namespace std;
int n,x;
int main(){
    cin>>n>>x;
    int cur=1;//从第一分钟开始处理
    //cur表示当前在处理第几分钟
    int ans=0;
    while (n--) {
        int l,r;
        cin>>l>>r;
        while (cur+x<=l) {
            cur+=x;
        }
        ans+=r-cur+1;
        cur=r+1;
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <unordered_set>
using namespace std;
int main() {
	int n, x;
	cin >> n >> x;
	int last = 1, ans = 0;
	for (int i = 0; i < n; i++) {
		int l, r;
		cin >> l >> r;
		ans += (l - last) % x + (r - l + 1);
		last = r + 1;
	}
	cout << ans;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<list>
#include<cmath>
#include<queue>
#include<cstring>
#include<map>
#include<set>
#include<fstream>
#include<time.h>

using namespace std;

int n, x;
int l[100000], r[100000];
int cur = 1, sum = 0;

int main()
{
	cin >> n >> x;
	for (int i = 1; i <= n; i++)
	{
		cin >> l[i] >> r[i];
	}
	for (int j = 1; j <= n; j++)
	{
		a:
		if (cur == l[j])
		{
			cur = r[j];
			sum += r[j] - l[j];
		}
		else if(cur<l[j])
		{
			if (l[j] - cur > x)
			{
				cur += x;
				if (l[j] - cur > x)
					goto a;
			}
			else if (l[j] - cur < x)
			{
				cur = r[j];
				sum += r[j] - cur;
			}
		}
	}
	cout << sum;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const db eps = 1e-6;
const int mod = 1e9+7;
const int maxn = 2e6+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;

int n, x;
int l[maxn], r[maxn];
int main() {
    scanf("%d %d", &n, &x);
    for(int i = 1; i <= n; i++) {
        scanf("%d %d", &l[i], &r[i]);
    }
    int ans = 0;
    int tmp = 1;
    int c = 1;
    while(1){

        if(tmp + x <= l[c]){
            tmp += x;
        }
        else {ans++;tmp++;}
        if(tmp > r[c]){
            c++;
            if(c > n)break;
        }
    }
    printf("%d", ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;

int n, x, l, r, t, res;

int main() {
	scanf("%d%d", &n, &x);
	t = 1;
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &l, &r);
		res += (l - t) % x;
		res += r - l + 1;
		t = r + 1;
	}
	printf("%d", res);
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int X,S,result=0,low,top,test=0,Q=1,M=1;
    cin >> S >> X;
    for (int i = 0; i < S; i++)
    {
        cin >> low >> top ;
        test = (low - Q) % X;
        low = low - test;
        Q = top+1;
        result += Q - low;
    }
cout << result;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int X,S,result=0,low,top,test=0,Q;
    cin >> S >> X;
	cin >> low >> top;
        test = low % X;
		result += (top-low) + test -1;
		Q = top;
		if (low <= X){result = top - low;}
    for (int i = 0; i < S-1; i++)
    {
        cin >> low >> top;
        test = (low-Q) % X;
		result += (top-low) + test;
		Q = top;
    }
cout << result+1;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int X,S,result=0,low,top,test=0,Q;
    cin >> S >> X;
    for (int i = 0; i < S; i++)
    {
        cin >> low >> top;
        Q = (low-test) % X;
        test = low - Q;
        result = result +( top - test);
        test = top;
    }
cout << result;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int X,S,result,low,top,test=0;
    cin >> S >> X;
    for (int i = 0; i < S; i++)
    {
        cin >> low >> top;
        while (test + X > low)
        {
            test += X;
        }
        result = top - test;
        test = top;
    }
cout << result;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int X,S,result=0,low,top,test=0,Q=0,M=1;
    cin >> S >> X;
    for (int i = 0; i < S; i++)
    {
        cin >> low >> top;
        while (Q + X < low)
        {
            Q += X;
        }
        low = Q;
        result += (top-low);
        Q = low;
    }
cout << result;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iterator>
using namespace std;
int main(){
	unsigned int n;
	unsigned long m;
	cin >> n >> m;
	vector < pair<unsigned long, unsigned long> > p(n);
	for (unsigned int i = 0; i < n; i++){
		cin >> p[i].first;
		cin >> p[i].second;
	}
	unsigned long long min = 0;
	unsigned long current = 1;
	for (unsigned int i = 0; i < n; i++){
		if (current + m <= p[i].first) {
			min += p[i].second + 1 - (current + m);
			current = p[i].second + 1;
		}
		else {
			min += p[i].second + 1 - p[i].first;
			current = p[i].second + 1;
		}
	}
	cout << min << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iterator>
using namespace std;
int main(){
	unsigned int n;
	unsigned long m;
	cin >> n >> m;
	vector < pair<unsigned long, unsigned long> > p(n);
	for (unsigned int i = 0; i < n; i++){
		cin >> p[i].first;
		cin >> p[i].second;
	}
	sort(p.begin(), p.end());
	unsigned long long min = 0;
	unsigned long current = 1;
	for (unsigned int i = 0; i < n; i++){
		if (current + m <= p[i].first && current + m <= (current - p[i].first) ) {
			min += p[i].second + 1 - (current + m);
			current = p[i].second + 1;
		}
		else {
			min += p[i].second + 1 - p[i].first + (p[i].first - current);
			current = p[i].second + 1;
		}
	}
	cout << min << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iterator>
using namespace std;
int main(){
	unsigned int n;
	unsigned long m;
	cin >> n >> m;
	vector < pair<unsigned long, unsigned long> > p(n);
	for (unsigned int i = 0; i < n; i++){
		cin >> p[i].first;
		cin >> p[i].second;
	}
	sort(p.begin(), p.end());
	unsigned long long min = 0;
	unsigned long current = 1;
	for (unsigned int i = 0; i < n; i++){
		unsigned long count = 1;
		if (current + m * count <= p[i].first ) {
			while ( (current + (m * count) ) < p[i].first){
				++count;
			}
			--count;
			if (count >= 2){
				min += p[i].second + 1 - ( current + (m *count) );
			}
			else {
				min += p[i].second + 1 - (current + m);
			}
		}
		else {
			min += p[i].second + 1 - p[i].first + (p[i].first - current);
		}
		current = p[i].second + 1;
	}
	cout << min << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iterator>
using namespace std;
int main(){
	unsigned int n;
	unsigned long m;
	cin >> n >> m;
	vector < pair<unsigned long, unsigned long> > p(n);
	for (unsigned int i = 0; i < n; i++){
		cin >> p[i].first;
		cin >> p[i].second;
	}
	unsigned long long min = 0;
	unsigned long current = 1;
	for (unsigned int i = 0; i < n; i++){
		if (current + m <= p[i].first && current + m <= (current - p[i].first) ) {
			min += p[i].second + 1 - (current + m);
			current = p[i].second + 1;
		}
		else {
			min += p[i].second + 1 - p[i].first + (p[i].first - current);
			current = p[i].second + 1;
		}
	}
	cout << min << endl;
	return 0;
}


import java.io.*;
import java.util.*;
public class Main
{
	final int p=128;
	final long mod=(long)(1e9+7);
	final int inf=(int)(1e9+1);
	private void solve()throws IOException
	{
		char s[]=(" "+nextLine()).toCharArray();
		int n=s.length-1;
		char t[]=(" "+nextLine()).toCharArray();
		int m=t.length-1;
		//map contains the hash,length pair of unique strings only
		HashMap<Long,Integer> map1=new HashMap<>();
		HashMap<Long,Integer> map2=new HashMap<>();
		init(s,n,map1);
		init(t,m,map2);
		int ans=inf;
		for(long l:map1.keySet())
			if(map2.containsKey(l) && map2.get(l)!=inf)
				ans=Math.min(ans,map1.get(l));
		out.println(ans==inf?-1:ans);
	}
	void init(char s[],int n,HashMap<Long,Integer> map)
	{
		for(int i=1;i<=n;i++)
		{
			long hash=0;
			for(int j=i;j<=n;j++)
			{
				hash=(((hash*p)%mod)+s[j])%mod;
				if(map.containsKey(hash))
					map.put(hash,inf);
				else
					map.put(hash,j-i+1);
			}
		}
	}

	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}


import java.io.*;
import java.util.*;
public class Main
{

	private void solve()throws IOException
	{
		char s[]=(" "+nextLine()).toCharArray();
		char t[]=(" "+nextLine()).toCharArray();
		int n=s.length-1;
		int m=t.length-1;
		//calculate lcp
		int lcp[][]=new int[n+1][m+1];
		for(int i=n;i>=1;i--)
			for(int j=m;j>=1;j--)
				if(s[i]!=t[j])
					lcp[i][j]=0;
				else
					lcp[i][j]=1+(i==n || j==m?0:lcp[i+1][j+1]);
		//dist[i] is the smallest distinct substring starting at i
		int dist1[]=new int[n+1];
		int selflcp1[][]=new int[n+1][n+1];
		for(int i=n;i>=1;i--)
		{
			for(int j=n;j>=1;j--)
			{
				if(j==i)
					continue;
				if(s[i]!=s[j])
					selflcp1[i][j]=0;
				else
					selflcp1[i][j]=1+(i==n || j==n?0:selflcp1[i+1][j+1]);
				dist1[i]=Math.max(dist1[i],selflcp1[i][j]);
			}
			dist1[i]++;
		}
		int dist2[]=new int[m+1];
		int selflcp2[][]=new int[m+1][m+1];
		for(int i=m;i>=1;i--)
		{
			for(int j=m;j>=1;j--)
			{
				if(j==i)
					continue;
				if(t[i]!=t[j])
					selflcp2[i][j]=0;
				else
					selflcp2[i][j]=1+(i==m || j==m?0:selflcp2[i+1][j+1]);
				dist2[i]=Math.max(dist2[i],selflcp2[i][j]);
			}
			dist2[i]++;
		}
		int ans=(int)(1e9+1);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				if(lcp[i][j]!=0 && lcp[i][j]>=Math.max(dist1[i],dist2[j]))
					ans=Math.min(ans,Math.max(dist1[i],dist2[j]));
		out.println(ans==(int)(1e9+1)?-1:ans);
	}

	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}


#include <bits/stdc++.h>

using namespace std;

template <class T,class U>
void maximize(T &lhs,U rhs){
    if(lhs < rhs)lhs = rhs;
}
template <class T,class U>
void minimize(T &lhs,U rhs){
    if(lhs > rhs)lhs = rhs;
}
template < class T,class U >
void maximize(T &lhs,const initializer_list < U > &rhs){
    U max_rhs = max(rhs);
    if(lhs < max_rhs){
        lhs = max_rhs;
    }
}
template < class T,class U >
void minimize(T &lhs,const initializer_list < U > &rhs){
    U min_rhs = min(rhs);
    if(lhs > min_rhs){
        lhs = min_rhs;
    }
}
template <class T>
T tabs(T x){
    return (x < (T)0 ? -x : x);
}

#define len(c) (int)((c).size())
#define llong long long
#define MASK(i) ((1LL) << (i))
#define BIT(x,i) (((x) >> (i)) & 1)
#define file_name "test"    /// FILE NAME HERE!!!

/* ================================= END OF TEMPLATE ================================= */

/* ================================= DEBUG ================================= */

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rand __rand
mt19937 generator(chrono::system_clock::now().time_since_epoch().count());
template <class T = int>
T rand(T range = numeric_limits <T> :: max()){
    return (T) (generator() % range);
}

/* ================================= DEBUG ================================= */

namespace Task{

    class SuffixArray{
        public:
            static const int lg = 13;
            vector < vector < int > > suff;
            vector < int > lcp;
            vector < int > ranks;
            vector < pair < pair < int,int >,int > > curRanks;
            int lstr = -1;
        public:
            SuffixArray(const string &str):lstr(len(str)){
                suff.resize(lg + 2,vector < int > (lstr + 2,0));
                curRanks.resize(lstr,{{0,0},0});
                ranks.resize(lstr,0);
                lcp.resize(lstr,0);
                for(int i = 0;i < lstr;++i){
                    suff[0][i] = str[i] -'a';
                }
                for(int j = 1;j <= lg;++j){
                    for(int i = 0;i < lstr;++i){
                        curRanks[i] = {{suff[j - 1][i],(i + MASK(j - 1) < lstr ? suff[j - 1][i + MASK(j - 1)] : -1)},i};
                    }
                    sort(curRanks.begin(),curRanks.end());
                    for(int i = 0;i < lstr;++i){
                        suff[j][curRanks[i].second] = (i and curRanks[i].first == curRanks[i - 1].first ? suff[j][curRanks[i - 1].second] : i);
                    }
                }
                for(int i = 0;i < lstr;++i){
                    ranks[i] = curRanks[i].second;
                }
            }

            int computeLcp(int rx,int ry){
                if(rx > ry){
                    swap(rx,ry);
                }
                int ret = 0;
                for(int j = lg;~j;--j){
                    if(ry + MASK(j) - 1 < lstr){
                        if(suff[j][rx] == suff[j][ry]){
                            ret |= MASK(j);
                            rx += MASK(j);
                            ry += MASK(j);
                        }
                    }
                }
                return ret;
            }

            void buildLcp(){
                for(int i = 0;i < lstr - 1;++i){
                    lcp[i] = computeLcp(ranks[i],ranks[i + 1]);
                }
            }
    };
    SuffixArray *sa;

    void solve(){
        string s,t;
        cin>>s>>t;
        sa = new SuffixArray(s + "#" + t);
        sa -> buildLcp();
        int ans = numeric_limits < int > :: max();
        auto ok = [&](int i,int j){
            if(i > j){
                swap(i,j);
            }
            return (0 <= i and i < len(s) and len(s) < j and j <= len(s) + len(t));
        };
        for(int i = 0;i < sa -> lstr - 1;++i){
            if(i == 0){
                if(sa -> lcp[i] > sa -> lcp[i + 1]){
                    if(ok(sa -> ranks[i],sa -> ranks[i + 1])){
                        minimize(ans,sa -> lcp[i + 1] + 1);
                    }
                }
            } else if(i == sa -> lstr - 2){
                if(sa -> lcp[i - 1] < sa -> lcp[i]){
                    if(ok(sa -> ranks[i],sa -> ranks[i + 1])){
                        minimize(ans,sa -> lcp[i - 1] + 1);
                    }
                }
            } else if(sa -> lcp[i - 1] < sa -> lcp[i] and sa -> lcp[i] > sa -> lcp[i + 1]){
                if(ok(sa -> ranks[i],sa -> ranks[i + 1])){
                    minimize(ans,1 + max(sa -> lcp[i - 1],sa -> lcp[i + 1]));
                }
            }
        }
        cout<<(ans == numeric_limits < int > :: max() ? -1 : ans)<<'\n';
    }
}

int main(int argc, char** argv){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(file_name".inp","r",stdin);
    //freopen(file_name".out","w",stdout);
    #endif // ONLINE_JUDGE
    Task::solve();
}


#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <string>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <list>
#include <map>
#include <bitset>
#include <queue>
#include <functional>
#include <numeric>
#include <assert.h>
#include <unordered_set>
#include <array>

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll>plll;
typedef pair<int, ll>pil;

const int MXn = (int)1e6 + 10;
int SA[MXn], T[MXn], rnk[MXn], lcp[MXn];
const int LOGN = 21, N = 1 << LOGN;
int rm[LOGN][N];
int LeadingZeros[MXn];

inline bool leq(int a1, int a2, int b1, int b2) // lexicographic order
{
	return(a1 < b1 || a1 == b1 && a2 <= b2);
} // for pairs
inline bool leq(int a1, int a2, int a3, int b1, int b2, int b3) {
	return(a1 < b1 || a1 == b1 && leq(a2, a3, b2, b3));
} // and triples
static void radixPass(int* a, int* b, int* r, int n, int K) {
	// count occurrences
	int* c = new int[K + 1]; // counter array
	for (int i = 0; i <= K; i++) c[i] = 0; // reset counters
	for (int i = 0; i < n; i++) c[r[a[i]]]++; // count occurrences
	for (int i = 0, sum = 0; i <= K; i++) // exclusive prefix sums
	{
		int t = c[i]; c[i] = sum; sum += t;
	}
	for (int i = 0; i < n; i++) b[c[r[a[i]]]++] = a[i]; // sort
	delete[] c;
}

//only if not GNU
void LZ(int n) {
	int Lzeros = 31;
	int j = 1;
	for (int i = 1; i <= n; i = (i << 1)) {
		while (j < (i << 1) && j <= n) {
			LeadingZeros[j] = Lzeros;
			j++;
		}
		Lzeros--;
	}
}

// K is alphabet size
void suffixArray(int* T, int* SA, int n, int K) {
	T[n] = T[n + 1] = T[n + 2] = 0;
	int n0 = (n + 2) / 3, n1 = (n + 1) / 3, n2 = n / 3, n02 = n0 + n2;
	int* R = new int[n02 + 3]; R[n02] = R[n02 + 1] = R[n02 + 2] = 0;
	int* SA12 = new int[n02 + 3]; SA12[n02] = SA12[n02 + 1] = SA12[n02 + 2] = 0;
	int* R0 = new int[n0];
	int* SA0 = new int[n0];

	for (int i = 0, j = 0; i < n + (n0 - n1); i++) if (i % 3 != 0) R[j++] = i;

	radixPass(R, SA12, T + 2, n02, K);
	radixPass(SA12, R, T + 1, n02, K);
	radixPass(R, SA12, T, n02, K);

	int name = 0, c0 = -1, c1 = -1, c2 = -1;
	for (int i = 0; i < n02; i++) {
		if (T[SA12[i]] != c0 || T[SA12[i] + 1] != c1 || T[SA12[i] + 2] != c2)
		{
			name++; c0 = T[SA12[i]]; c1 = T[SA12[i] + 1]; c2 = T[SA12[i] + 2];
		}
		if (SA12[i] % 3 == 1) { R[SA12[i] / 3] = name; } // write to R1
		else { R[SA12[i] / 3 + n0] = name; } // write to R2
	}

	if (name < n02) {
		suffixArray(R, SA12, n02, name);
		for (int i = 0; i < n02; i++) R[SA12[i]] = i + 1;
	}
	else
		for (int i = 0; i < n02; i++) SA12[R[i] - 1] = i;

	for (int i = 0, j = 0; i < n02; i++) if (SA12[i] < n0) R0[j++] = 3 * SA12[i];
	radixPass(R0, SA0, T, n0, K);

	for (int p = 0, t = n0 - n1, k = 0; k < n; k++) {
#define GetI() (SA12[t] < n0 ? SA12[t] * 3 + 1 : (SA12[t] - n0) * 3 + 2)
		int i = GetI();
		int j = SA0[p];
		if (SA12[t] < n0 ?
			leq(T[i], R[SA12[t] + n0], T[j], R[j / 3]) :
			leq(T[i], T[i + 1], R[SA12[t] - n0 + 1], T[j], T[j + 1], R[j / 3 + n0]))
		{
			SA[k] = i; t++;
			if (t == n02)
				for (k++; p < n0; p++, k++) SA[k] = SA0[p];
		}
		else {
			SA[k] = j; p++;
			if (p == n0)
				for (k++; t < n02; t++, k++) SA[k] = GetI();
		}
	}
	delete[] R; delete[] SA12; delete[] SA0; delete[] R0;
}
void GetLcp(int n) {
	for (int i = 0; i < n; i++) rnk[SA[i]] = i;
	for (int i = 0, k = 0; i < n; i++) {
		if (k > 0) k--;
		if (rnk[i] == n - 1) { lcp[n - 1] = -1; k = 0; continue; }
		int j = SA[rnk[i] + 1];
		while (T[i + k] == T[j + k]) k++;
		lcp[rnk[i]] = k;
	}
}

// if this is for the suffix array: initialization: init(s); init(lcp, SZ(s));
void init(int* tab, int n) { // tab[0..n-1]
	LZ(n);
	for (int i = 0; i < n; i++) rm[0][i] = tab[i];
	for (int k = 1; k < LOGN; k++)
		for (int i = 0; i <= n - (1 << k); i++)
			rm[k][i] = std::min(rm[k - 1][i],
				rm[k - 1][i + (1 << (k - 1))]);
}
int query(int a, int b) {
	int k = 31 - LeadingZeros[b - a + 1];//__builtin_clz(b - a + 1) if working on linux
	return std::min(rm[k][a], rm[k][b - (1 << k) + 1]);
}
int lcpBetween(int a, int b, int size) { // assume: a != b, a and b indices T // a and b indices in the original string
	if (a == b) return size - a;
	a = rnk[a], b = rnk[b];
	if (a > b) std::swap(a, b);
	return query(a, b - 1);
}

//init suffix array
void InitSuffixArray(string &s) {
	set<char> st;
	for (auto it : s) {
		st.insert(it);
	}

	map<char, int> charRank;
	int cur = 1;
	for (auto it : st) {
		charRank[it] = cur++;
	}

	for (int i = 0; i < s.size(); i++) {
		T[i] = charRank[s[i]];
	}

	int alphabetSize = st.size() + 1;
	suffixArray(T, SA, s.size(), alphabetSize);
	GetLcp(s.size());
	init(lcp, s.size());
}

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	string s1, s2;
	cin >> s1 >> s2;

	string concat = s1 + "$" + s2;
	InitSuffixArray(concat);

	int ans = -1;
	for (int i = 0; i < concat.size() - 1; i++) {
		int x = ((i == 0) ? 0 : lcp[i - 1]);
		int z = lcp[i];
		int y = ((i + 2 == concat.size())? 0 :lcp[i + 1]);

		bool valid = SA[i] < s1.size() && SA[i + 1] > s1.size();
		valid = valid || (SA[i] > s1.size() && SA[i + 1] < s1.size());

		if (valid && (z > max(x, y))) {
			ans = ((ans == -1) ? max(x, y) + 1 : min(ans, max(x, y) + 1));
		}
	}

	cout << ans << "\n";
}


#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <string>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <list>
#include <map>
#include <bitset>
#include <queue>
#include <functional>
#include <numeric>
#include <assert.h>
#include <unordered_set>
#include <array>

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll>plll;
typedef pair<int, ll>pil;

struct RabinKarp { // Rabin Karp
	const int mod[2] = { (int)1e9 + 7, (int)1e9 + 9 };
	const int prime[2] = { 179, 233 };
	vector<vll> pPow, h;

	RabinKarp(string& s) {
		pPow.resize(s.size() + 9, vll(2));
		h.resize(s.size() + 9, vll(2));
		init(s);
	}

	void init(const string& s) {
		if (!pPow[0][0]) {
			pPow[0][0] = pPow[0][1] = 1;
			for (int i = 1; i <= s.size(); ++i)
				for (int z = 0; z < 2; ++z)
					pPow[i][z] = pPow[i - 1][z] * prime[z] % mod[z];
		}
		for (int i = 0; i < (int)s.size(); ++i)
			for (int z = 0; z < 2; ++z)
				h[i + 1][z] = (h[i][z] * prime[z] + s[i]) % mod[z];
	}

	array<ll, 2> getHash(const string & s) {
		array<ll, 2> curhash = { 0, 0 };
		for (int i = 0; i < (int)s.size(); ++i)
			for (int z = 0; z < 2; ++z)
				curhash[z] = (curhash[z] * prime[z] + s[i]) % mod[z];

		return curhash;
	}

	array<ll, 2> query(const int l, const int r) {
		array<ll, 2> res;
		for (int z = 0; z < 2; ++z) {
			res[z] = (h[r + 1][z] - h[l][z] * pPow[r - l + 1][z]) % mod[z];
			if (res[z] < 0)
				res[z] += mod[z];
		}
		return res;
	}

};

ll to_ll(array<ll, 2> a) {
	return ((a[0] << 32) + a[1]);
}

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	string s1, s2;
	cin >> s1 >> s2;

	RabinKarp rk1(s1), rk2(s2);

	unordered_map<ll, pii> occ1, occ2;

	for (int l = 0; l < s1.size(); l++) {
		for (int r = l; r < s1.size(); r++) {
			ll hash = to_ll(rk1.query(l, r));
			if (occ1.count(hash) == 0) occ1[hash] = { 0, r - l + 1 };
			occ1[hash].first++;
		}
	}

	for (int l = 0; l < s2.size(); l++) {
		for (int r = l; r < s2.size(); r++) {
			ll hash = to_ll(rk2.query(l, r));
			if (occ2.count(hash) == 0) occ2[hash] = { 0, r - l + 1 };
			occ2[hash].first++;
		}
	}

	int minimum = -1;
	for (auto it : occ1) {
		ll word = it.first;
		int cnt = it.second.first;
		int len = it.second.second;

		if (cnt == 1 && occ2.count(word) > 0) {
			if (occ2[word].first == 1) {
				minimum = ((minimum == -1) ? len : min(minimum, len));
			}
		}
	}
	cout << minimum << "\n";
}


#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <string>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <list>
#include <map>
#include <bitset>
#include <queue>
#include <functional>
#include <numeric>
#include <assert.h>
#include <unordered_set>
#include <array>

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll>plll;
typedef pair<int, ll>pil;

const int MXn = (int)1e6 + 10;
int SA[MXn], T[MXn], rnk[MXn], lcp[MXn];
const int LOGN = 21, N = 1 << LOGN;
int rm[LOGN][N];
int LeadingZeros[MXn];

inline bool leq(int a1, int a2, int b1, int b2) // lexicographic order
{
	return(a1 < b1 || a1 == b1 && a2 <= b2);
} // for pairs
inline bool leq(int a1, int a2, int a3, int b1, int b2, int b3) {
	return(a1 < b1 || a1 == b1 && leq(a2, a3, b2, b3));
} // and triples
static void radixPass(int* a, int* b, int* r, int n, int K) {
	// count occurrences
	int* c = new int[K + 1]; // counter array
	for (int i = 0; i <= K; i++) c[i] = 0; // reset counters
	for (int i = 0; i < n; i++) c[r[a[i]]]++; // count occurrences
	for (int i = 0, sum = 0; i <= K; i++) // exclusive prefix sums
	{
		int t = c[i]; c[i] = sum; sum += t;
	}
	for (int i = 0; i < n; i++) b[c[r[a[i]]]++] = a[i]; // sort
	delete[] c;
}

//only if not GNU
void LZ(int n) {
	int Lzeros = 31;
	int j = 1;
	for (int i = 1; i <= n; i = (i << 1)) {
		while (j < (i << 1) && j <= n) {
			LeadingZeros[j] = Lzeros;
			j++;
		}
		Lzeros--;
	}
}

// K is alphabet size
void suffixArray(int* T, int* SA, int n, int K) {
	T[n] = T[n + 1] = T[n + 2] = 0;
	int n0 = (n + 2) / 3, n1 = (n + 1) / 3, n2 = n / 3, n02 = n0 + n2;
	int* R = new int[n02 + 3]; R[n02] = R[n02 + 1] = R[n02 + 2] = 0;
	int* SA12 = new int[n02 + 3]; SA12[n02] = SA12[n02 + 1] = SA12[n02 + 2] = 0;
	int* R0 = new int[n0];
	int* SA0 = new int[n0];

	for (int i = 0, j = 0; i < n + (n0 - n1); i++) if (i % 3 != 0) R[j++] = i;

	radixPass(R, SA12, T + 2, n02, K);
	radixPass(SA12, R, T + 1, n02, K);
	radixPass(R, SA12, T, n02, K);

	int name = 0, c0 = -1, c1 = -1, c2 = -1;
	for (int i = 0; i < n02; i++) {
		if (T[SA12[i]] != c0 || T[SA12[i] + 1] != c1 || T[SA12[i] + 2] != c2)
		{
			name++; c0 = T[SA12[i]]; c1 = T[SA12[i] + 1]; c2 = T[SA12[i] + 2];
		}
		if (SA12[i] % 3 == 1) { R[SA12[i] / 3] = name; } // write to R1
		else { R[SA12[i] / 3 + n0] = name; } // write to R2
	}

	if (name < n02) {
		suffixArray(R, SA12, n02, name);
		for (int i = 0; i < n02; i++) R[SA12[i]] = i + 1;
	}
	else
		for (int i = 0; i < n02; i++) SA12[R[i] - 1] = i;

	for (int i = 0, j = 0; i < n02; i++) if (SA12[i] < n0) R0[j++] = 3 * SA12[i];
	radixPass(R0, SA0, T, n0, K);

	for (int p = 0, t = n0 - n1, k = 0; k < n; k++) {
#define GetI() (SA12[t] < n0 ? SA12[t] * 3 + 1 : (SA12[t] - n0) * 3 + 2)
		int i = GetI();
		int j = SA0[p];
		if (SA12[t] < n0 ?
			leq(T[i], R[SA12[t] + n0], T[j], R[j / 3]) :
			leq(T[i], T[i + 1], R[SA12[t] - n0 + 1], T[j], T[j + 1], R[j / 3 + n0]))
		{
			SA[k] = i; t++;
			if (t == n02)
				for (k++; p < n0; p++, k++) SA[k] = SA0[p];
		}
		else {
			SA[k] = j; p++;
			if (p == n0)
				for (k++; t < n02; t++, k++) SA[k] = GetI();
		}
	}
	delete[] R; delete[] SA12; delete[] SA0; delete[] R0;
}
void GetLcp(int n) {
	for (int i = 0; i < n; i++) rnk[SA[i]] = i;
	for (int i = 0, k = 0; i < n; i++) {
		if (k > 0) k--;
		if (rnk[i] == n - 1) { lcp[n - 1] = -1; k = 0; continue; }
		int j = SA[rnk[i] + 1];
		while (T[i + k] == T[j + k]) k++;
		lcp[rnk[i]] = k;
	}
}

// if this is for the suffix array: initialization: init(s); init(lcp, SZ(s));
void init(int* tab, int n) { // tab[0..n-1]
	LZ(n);
	for (int i = 0; i < n; i++) rm[0][i] = tab[i];
	for (int k = 1; k < LOGN; k++)
		for (int i = 0; i <= n - (1 << k); i++)
			rm[k][i] = std::min(rm[k - 1][i],
				rm[k - 1][i + (1 << (k - 1))]);
}
int query(int a, int b) {
	int k = 31 - LeadingZeros[b - a + 1];//__builtin_clz(b - a + 1) if working on linux
	return std::min(rm[k][a], rm[k][b - (1 << k) + 1]);
}
int lcpBetween(int a, int b, int size) { // assume: a != b, a and b indices T // a and b indices in the original string
	if (a == b) return size - a;
	a = rnk[a], b = rnk[b];
	if (a > b) std::swap(a, b);
	return query(a, b - 1);
}

//init suffix array
void InitSuffixArray(string &s) {
	set<char> st;
	for (auto it : s) {
		st.insert(it);
	}

	map<char, int> charRank;
	int cur = 1;
	for (auto it : st) {
		charRank[it] = cur++;
	}

	for (int i = 0; i < s.size(); i++) {
		T[i] = charRank[s[i]];
	}

	int alphabetSize = st.size() + 1;
	suffixArray(T, SA, s.size(), alphabetSize);
	GetLcp(s.size());
	init(lcp, s.size());
}

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	string s1, s2;
	cin >> s1 >> s2;

	string concat = s1 + "$" + s2;
	InitSuffixArray(concat);

	int ans = -1;
	for (int i = 0; i < concat.size(); i++) {
		int x = ((i == 0) ? 0 : lcp[i - 1]);
		int z = lcp[i];
		int y = ((i == concat.size() - 1) ? 0 : lcp[i + 1]);

		bool valid = SA[i] < s1.size() && SA[i + 1] > s1.size();
		valid = valid | (SA[i] > s1.size() && SA[i + 1] < s1.size());

		if (valid && (z > max(x, y))) {
			ans = ((ans == -1) ? z : min(ans, max(x, y) + 1));
		}
	}

	cout << ans << "\n";
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<set>
#include<unordered_map>
#include<iomanip>
#include<set>
#include<functional>
#include<numeric>
#include<map>
#include<math.h>
#include<stack>
#include <stdio.h>
#include <string.h>

using namespace std;

typedef std::pair<int, int> pii;
typedef long long ll;
typedef std::vector<int>vi;
typedef std::pair<pii, int> piii;

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	unordered_map<string, int> occ1, occ2;
	string s1, s2;
	cin >> s1 >> s2;

	for (int i = 0; i < s1.size(); i++) {
		string temp = "";
		for (int j = i; j < s1.size(); j++) {
			temp.push_back(s1[j]);
			occ1[temp]++;
		}
	}

	for (int i = 0; i < s2.size(); i++) {
		string temp = "";
		for (int j = i; j < s2.size(); j++) {
			temp.push_back(s2[j]);
			occ2[temp]++;
		}
	}

	int ans = -1;

	for (auto it : occ1) {
		if (it.second == 1) {
			if (occ2.count(it.first) > 0 && occ2[it.first] == 1) {
				if (ans == -1) ans = it.first.size();
				else ans = min(ans, (int)it.first.size());
			}
		}
	}

	cout << ans << "\n";
}


#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <string>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <list>
#include <map>
#include <bitset>
#include <queue>
#include <functional>
#include <numeric>
#include <assert.h>
#include <unordered_set>
#include <array>

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll>plll;
typedef pair<int, ll>pil;

struct RabinKarp { // Rabin Karp
	const int mod[2] = { (int)1e9 + 7, (int)1e9 + 9 };
	const int prime[2] = { 179, 233 };
	vector<vll> pPow, h;

	RabinKarp(string& s) {
		pPow.resize(s.size() + 9, vll(2));
		h.resize(s.size() + 9, vll(2));
		init(s);
	}

	void init(const string& s) {
		if (!pPow[0][0]) {
			pPow[0][0] = pPow[0][1] = 1;
			for (int i = 1; i <= s.size(); ++i)
				for (int z = 0; z < 2; ++z)
					pPow[i][z] = pPow[i - 1][z] * prime[z] % mod[z];
		}
		for (int i = 0; i < (int)s.size(); ++i)
			for (int z = 0; z < 2; ++z)
				h[i + 1][z] = (h[i][z] * prime[z] + s[i]) % mod[z];
	}

	array<ll, 2> getHash(const string & s) {
		array<ll, 2> curhash = { 0, 0 };
		for (int i = 0; i < (int)s.size(); ++i)
			for (int z = 0; z < 2; ++z)
				curhash[z] = (curhash[z] * prime[z] + s[i]) % mod[z];

		return curhash;
	}

	array<ll, 2> query(const int l, const int r) {
		array<ll, 2> res;
		for (int z = 0; z < 2; ++z) {
			res[z] = (h[r + 1][z] - h[l][z] * pPow[r - l + 1][z]) % mod[z];
			if (res[z] < 0)
				res[z] += mod[z];
		}
		return res;
	}

};

ll to_ll(array<ll, 2> a) {
	return ((a[0] << 32) + a[1]);
}

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	string s1, s2;
	cin >> s1 >> s2;

	RabinKarp rk1(s1), rk2(s2);

	map<ll, pii> occ1, occ2;

	for (int l = 0; l < s1.size(); l++) {
		for (int r = l; r < s1.size(); r++) {
			ll hash = to_ll(rk1.query(l, r));
			if (occ1.count(hash) == 0) occ1[hash] = { 0, r - l + 1 };
			occ1[hash].first++;
		}
	}

	for (int l = 0; l < s2.size(); l++) {
		for (int r = l; r < s2.size(); r++) {
			ll hash = to_ll(rk2.query(l, r));
			if (occ2.count(hash) == 0) occ2[hash] = { 0, r - l + 1 };
			occ2[hash].first++;
		}
	}

	int minimum = -1;
	for (auto it : occ1) {
		ll word = it.first;
		int cnt = it.second.first;
		int len = it.second.second;

		if (cnt == 1 && occ2.count(word) > 0) {
			if (occ2[word].first == 1) {
				minimum = ((minimum == -1) ? len : min(minimum, len));
			}
		}
	}
	cout << minimum << "\n";
}


#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <string>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <list>
#include <map>
#include <bitset>
#include <queue>
#include <functional>
#include <numeric>
#include <assert.h>
#include <unordered_set>
#include <array>

#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (auto i = t.begin(); i != t.end(); ++i)
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef pair<ll, pll>plll;
typedef pair<int, ll>pil;

const int MXn = (int)1e6 + 10;
int SA[MXn], T[MXn], rnk[MXn], lcp[MXn];
const int LOGN = 21, N = 1 << LOGN;
int rm[LOGN][N];
int LeadingZeros[MXn];

inline bool leq(int a1, int a2, int b1, int b2) // lexicographic order
{
	return(a1 < b1 || a1 == b1 && a2 <= b2);
} // for pairs
inline bool leq(int a1, int a2, int a3, int b1, int b2, int b3) {
	return(a1 < b1 || a1 == b1 && leq(a2, a3, b2, b3));
} // and triples
static void radixPass(int* a, int* b, int* r, int n, int K) {
	// count occurrences
	int* c = new int[K + 1]; // counter array
	for (int i = 0; i <= K; i++) c[i] = 0; // reset counters
	for (int i = 0; i < n; i++) c[r[a[i]]]++; // count occurrences
	for (int i = 0, sum = 0; i <= K; i++) // exclusive prefix sums
	{
		int t = c[i]; c[i] = sum; sum += t;
	}
	for (int i = 0; i < n; i++) b[c[r[a[i]]]++] = a[i]; // sort
	delete[] c;
}

//only if not GNU
void LZ(int n) {
	int Lzeros = 31;
	int j = 1;
	for (int i = 1; i <= n; i = (i << 1)) {
		while (j < (i << 1) && j <= n) {
			LeadingZeros[j] = Lzeros;
			j++;
		}
		Lzeros--;
	}
}

// K is alphabet size
void suffixArray(int* T, int* SA, int n, int K) {
	T[n] = T[n + 1] = T[n + 2] = 0;
	int n0 = (n + 2) / 3, n1 = (n + 1) / 3, n2 = n / 3, n02 = n0 + n2;
	int* R = new int[n02 + 3]; R[n02] = R[n02 + 1] = R[n02 + 2] = 0;
	int* SA12 = new int[n02 + 3]; SA12[n02] = SA12[n02 + 1] = SA12[n02 + 2] = 0;
	int* R0 = new int[n0];
	int* SA0 = new int[n0];

	for (int i = 0, j = 0; i < n + (n0 - n1); i++) if (i % 3 != 0) R[j++] = i;

	radixPass(R, SA12, T + 2, n02, K);
	radixPass(SA12, R, T + 1, n02, K);
	radixPass(R, SA12, T, n02, K);

	int name = 0, c0 = -1, c1 = -1, c2 = -1;
	for (int i = 0; i < n02; i++) {
		if (T[SA12[i]] != c0 || T[SA12[i] + 1] != c1 || T[SA12[i] + 2] != c2)
		{
			name++; c0 = T[SA12[i]]; c1 = T[SA12[i] + 1]; c2 = T[SA12[i] + 2];
		}
		if (SA12[i] % 3 == 1) { R[SA12[i] / 3] = name; } // write to R1
		else { R[SA12[i] / 3 + n0] = name; } // write to R2
	}

	if (name < n02) {
		suffixArray(R, SA12, n02, name);
		for (int i = 0; i < n02; i++) R[SA12[i]] = i + 1;
	}
	else
		for (int i = 0; i < n02; i++) SA12[R[i] - 1] = i;

	for (int i = 0, j = 0; i < n02; i++) if (SA12[i] < n0) R0[j++] = 3 * SA12[i];
	radixPass(R0, SA0, T, n0, K);

	for (int p = 0, t = n0 - n1, k = 0; k < n; k++) {
#define GetI() (SA12[t] < n0 ? SA12[t] * 3 + 1 : (SA12[t] - n0) * 3 + 2)
		int i = GetI();
		int j = SA0[p];
		if (SA12[t] < n0 ?
			leq(T[i], R[SA12[t] + n0], T[j], R[j / 3]) :
			leq(T[i], T[i + 1], R[SA12[t] - n0 + 1], T[j], T[j + 1], R[j / 3 + n0]))
		{
			SA[k] = i; t++;
			if (t == n02)
				for (k++; p < n0; p++, k++) SA[k] = SA0[p];
		}
		else {
			SA[k] = j; p++;
			if (p == n0)
				for (k++; t < n02; t++, k++) SA[k] = GetI();
		}
	}
	delete[] R; delete[] SA12; delete[] SA0; delete[] R0;
}
void GetLcp(int n) {
	for (int i = 0; i < n; i++) rnk[SA[i]] = i;
	for (int i = 0, k = 0; i < n; i++) {
		if (k > 0) k--;
		if (rnk[i] == n - 1) { lcp[n - 1] = -1; k = 0; continue; }
		int j = SA[rnk[i] + 1];
		while (T[i + k] == T[j + k]) k++;
		lcp[rnk[i]] = k;
	}
}

// if this is for the suffix array: initialization: init(s); init(lcp, SZ(s));
void init(int* tab, int n) { // tab[0..n-1]
	LZ(n);
	for (int i = 0; i < n; i++) rm[0][i] = tab[i];
	for (int k = 1; k < LOGN; k++)
		for (int i = 0; i <= n - (1 << k); i++)
			rm[k][i] = std::min(rm[k - 1][i],
				rm[k - 1][i + (1 << (k - 1))]);
}
int query(int a, int b) {
	int k = 31 - LeadingZeros[b - a + 1];//__builtin_clz(b - a + 1) if working on linux
	return std::min(rm[k][a], rm[k][b - (1 << k) + 1]);
}
int lcpBetween(int a, int b, int size) { // assume: a != b, a and b indices T // a and b indices in the original string
	if (a == b) return size - a;
	a = rnk[a], b = rnk[b];
	if (a > b) std::swap(a, b);
	return query(a, b - 1);
}

//init suffix array
void InitSuffixArray(string &s) {
	set<char> st;
	for (auto it : s) {
		st.insert(it);
	}

	map<char, int> charRank;
	int cur = 1;
	for (auto it : st) {
		charRank[it] = cur++;
	}

	for (int i = 0; i < s.size(); i++) {
		T[i] = charRank[s[i]];
	}

	int alphabetSize = st.size() + 1;
	suffixArray(T, SA, s.size(), alphabetSize);
	GetLcp(s.size());
	init(lcp, s.size());
}

int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	string s1, s2;
	cin >> s1 >> s2;

	string concat = s1 + "$" + s2;
	InitSuffixArray(concat);

	int ans = -1;
	for (int i = 0; i < concat.size() - 1; i++) {
		int x = ((i == 0) ? 0 : lcp[i - 1]);
		int z = lcp[i];
		int y = ((i == concat.size() - 1) ? 0 : lcp[i + 1]);

		bool valid = SA[i] < s1.size() && SA[i + 1] > s1.size();
		valid = valid | (SA[i] > s1.size() && SA[i + 1] < s1.size());

		if (valid && (z > max(x, y))) {
			ans = ((ans == -1) ? z : min(ans, max(x, y) + 1));
		}
	}

	cout << ans << "\n";
}


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

const int N = (int)2e5 + 5;
const int inf = (int)1e9;

struct SAM {
  int tot, root, last, link[N * 2], len[N * 2], nxt[N * 2][27];
  int cnt[N * 2], mn[N * 2], mx[N * 2], linkcnt[N * 2];

  int newNode() {
    int res = tot++;
    memset(nxt[res], 0, sizeof(nxt[res]));
    link[res] = len[res] = 0;
    cnt[res] = 0; mn[res] = inf; mx[res] = -1; linkcnt[res] = 0;
    return res;
  }
  void init() {
    tot = 1;
    root = newNode();
    last = root;
  }
  void upd(int u, int lu) {
    if(link[u]) linkcnt[link[u]]--;
    link[u] = lu;
    linkcnt[lu]++;
  }
  void push(int c, int pos) {
    int p = last, np = newNode();
    len[np] = len[p] + 1;
    for(; p && nxt[p][c] == 0 ; p = link[p]) nxt[p][c] = np;

    if(!p) upd(np, root);
    else {
      int q = nxt[p][c];
      if(len[p] + 1 == len[q]) upd(np, q);
      else {
        int nq = newNode();
        memcpy(nxt[nq], nxt[q], sizeof(nxt[q]));
        len[nq] = len[p] + 1;
        upd(nq, link[q]); upd(q, nq); upd(np, nq);
        for(; p && nxt[p][c] == q ; p = link[p]) nxt[p][c] = nq;
      }
    }
    cnt[np] = 1;
    mn[np] = mx[np] = pos;
    last = np;
  }
  void push(string& s) {
    for(int i = 0 ; i < (int)s.size() ; ++i) push(s[i] - 'a', i + 1);
  }
  void build() {
    queue<int> q;
    for(int i = 1 ; i < tot ; ++i) if(!linkcnt[i]) q.push(i);
    while(!q.empty()) {
      int p = q.front(), lp = link[p]; q.pop();
      cnt[lp] += cnt[p];
      mn[lp] = min(mn[lp], mn[p]);
      mx[lp] = max(mx[lp], mx[p]);
      linkcnt[lp]--;
      if(!linkcnt[lp]) q.push(lp);
    }
  }
  int solve(int l) {
    int ans = inf;
    for(int i = 1 ; i < tot ; ++i) {
      if(mn[i] < l && mx[i] > l && cnt[i] == 2) {
        ans = min(ans, len[link[i]] + 1);
      }
    }
    return ans == inf ? -1 : ans;
  }
} sam;

string s, t;

void init() {
  cin >> s >> t;
}
void solve() {
  sam.init();
  string tmp = s + '{' + t;
  sam.push(tmp);
  sam.build();
  cout << sam.solve((int)s.size()) << '\n';
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  init();
  solve();
}


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

const int N = (int)2e5 + 5;
const int inf = (int)1e9;

struct SAM {
  int tot, root, last, link[N * 2], len[N * 2], nxt[N * 2][27];
  int cnt[N * 2], mn[N * 2], mx[N * 2], linkcnt[N * 2];

  int newNode() {
    int res = tot++;
    memset(nxt[res], 0, sizeof(nxt[res]));
    link[res] = len[res] = 0;
    cnt[res] = 0; mn[res] = inf; mx[res] = -1; linkcnt[res] = 0;
    return res;
  }
  void init() {
    tot = 1;
    root = newNode();
    last = root;
  }
  void upd(int u, int lu) {
    if(link[u]) linkcnt[link[u]]--;
    link[u] = lu;
    linkcnt[lu]++;
  }
  void push(int c, int pos) {
    int p = last, np = newNode();
    len[np] = len[p] + 1;
    for(; p && nxt[p][c] == 0 ; p = link[p]) nxt[p][c] = np;

    if(!p) upd(np, root);
    else {
      int q = nxt[p][c];
      if(len[p] + 1 == len[q]) upd(np, q);
      else {
        int nq = newNode();
        memcpy(nxt[nq], nxt[q], sizeof(nxt[q]));
        len[nq] = len[p] + 1;
        upd(nq, link[q]); upd(q, nq); upd(np, nq);
        for(; p && nxt[p][c] == q ; p = link[p]) nxt[p][c] = nq;
      }
    }
    cnt[np] = 1;
    mn[np] = mx[np] = pos;
    last = np;
  }
  void push(string& s) {
    for(int i = 0 ; i < (int)s.size() ; ++i) push(s[i] - 'a', i + 1);
  }
  void build() {
    queue<int> q;
    for(int i = 1 ; i < tot ; ++i) if(!linkcnt[i]) q.push(i);
    while(!q.empty()) {
      int p = q.front(), lp = link[p]; q.pop();
      cnt[lp] += cnt[p];
      mn[lp] = min(mn[lp], mn[p]);
      mx[lp] = max(mx[lp], mx[p]);
      linkcnt[lp]--;
      if(!linkcnt[lp]) q.push(lp);
    }
  }
  int solve(int l) {
    int ans = inf;
    for(int i = 1 ; i < tot ; ++i) {
      if(mn[i] < l && mx[i] > l && cnt[i] == 2) {
        ans = min(ans, len[link[i]] + 1);
      }
    }
    return ans == inf ? -1 : ans;
  }
} sam;

string s, t;

void init() {
  cin >> s >> t;
}
void solve() {
  sam.init();
  string tmp = s + '{' + t;
  sam.push(tmp);
  sam.build();
  cout << sam.solve((int)s.size() + 1) << '\n';
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  init();
  solve();
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 5005;
ULL prime = 3797;
ULL F[N],h1[N],h2[N];

ULL seg1(int x, int y) {
    return h1[y] - F[y - x + 1] * h1[x - 1];
}
ULL seg2(int x, int y) {
    return h2[y] - F[y - x + 1] * h2[x - 1];
}
char s1[N],s2[N];
vector<pair<ULL,int>>save[2][66600];
vector<ULL>v1;
void add(int id,ULL val)
{
    unsigned short int x = val;
    if(id == 0)v1.push_back(val);
    for(int i = 0;i < save[id][x].size();i++){
        if(save[id][x][i].first == val){
            save[id][x][i].second++;
            return;
        }
    }
    save[id][x].push_back(mk(val,1));
}
int figure(int id,ULL val)
{
    unsigned short int x = val;
    for(int i = 0;i < save[id][x].size();i++){
        if(save[id][x][i].first == val){
            if(save[id][x][i].second == 1)return 1;
            else return 0;
        }
    }
    return 0;
}
void CLEAR(int id,ULL val)
{
    unsigned short int x = val;
    save[id][x].clear();
}
int main()
{
    F[0] = 1; for(int i = 1; i < N; i++) F[i] = F[i - 1] * prime;

    scanf("%s",s1 + 1);
    scanf("%s",s2 + 1);
    int n = strlen(s1 + 1);
    int m = strlen(s2 + 1);

    h1[0] = 0; FOR(i, 1, n + 1) h1[i] = h1[i - 1] * prime + s1[i];
    h2[0] = 0; FOR(i, 1, m + 1) h2[i] = h2[i - 1] * prime + s2[i];

    int ans = -1;
    for(int mid = 1;mid <= min(n,m);mid++)
    {
        v1.clear();
//        cout << "here\n";
        for(int i = 1;i + mid - 1 <= n;i++){
            add(0,seg1(i,i+mid-1));
        }
//        cout << "here\n";
        for(int i = 1;i + mid - 1 <= m;i++){
            add(1,seg2(i,i+mid-1));
        }
//        cout << "here\n";
        for(int i = 0;i < v1.size();i++){
            if(figure(0,v1[i]) and figure(1,v1[i])){
                ans = mid;
                break;
            }
        }
//        cout << "here\n";
        if(ans != -1)break;

        for(int i = 1;i + mid - 1 <= n;i++){
            CLEAR(0,seg1(i,i+mid-1));
        }
        for(int i = 1;i + mid - 1 <= m;i++){
            CLEAR(1,seg2(i,i+mid-1));
        }
//        cout << "here\n";
    }
    cout << ans << "\n";
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 5005;
unordered_map<int,int>m1,m2,M;
ULL prime = 3797;
ULL F[N],h1[N],h2[N];

ULL seg1(int x, int y) {
    return h1[y] - F[y - x + 1] * h1[x - 1];
}
ULL seg2(int x, int y) {
    return h2[y] - F[y - x + 1] * h2[x - 1];
}
char s1[N],s2[N];

int main()
{
    F[0] = 1; for(int i = 1; i < N; i++) F[i] = F[i - 1] * prime;

    scanf("%s",s1 + 1);
    scanf("%s",s2 + 1);
    int n = strlen(s1 + 1);
    int m = strlen(s2 + 1);

    h1[0] = 0; FOR(i, 1, n + 1) h1[i] = h1[i - 1] * prime + s1[i];
    h2[0] = 0; FOR(i, 1, m + 1) h2[i] = h2[i - 1] * prime + s2[i];

    int ans = -1;
    for(int mid = 1;mid <= min(n,m);mid++)
    {
        m1.clear();m2.clear();
        M.clear();
        for(int i = 1;i + mid - 1 <= n;i++){
            M[seg1(i,i+mid-1)]++;
        }
        for(auto it = M.begin();it!=M.end();it++){
            if(it->ss == 1)m1[it->ff] = 1;
        }
        M.clear();
        for(int i = 1;i + mid - 1 <= m;i++){
            M[seg2(i,i+mid-1)]++;
        }
        for(auto it = M.begin();it!=M.end();it++){
            if(it->ss == 1)m2[it->ff] = 1;
        }
        if(m1.size() > m2.size())swap(m1,m2);
        bool ok = 0;
        for(auto it = m1.begin();it!=m1.end();it++){
            if(it->ss != 1)continue;
            if(m2[it->ff] == 1){
                ok = 1;break;
            }
        }
        if(ok == 1){
            ans = mid;
            break;
        }
    }
    cout << ans << "\n";
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 5005;
map<ULL,int>m1[N],m2[N];
ULL prime = 3797;
int main()
{
    string s1,s2;
    cin >> s1 >> s2;
    for(int i = 0;i < s1.size();i++){
        ULL Hash = 0;
        for(int j = i;j < s1.size();j++){
            Hash = Hash * prime + s1[j];
            m1[j - i + 1][Hash]++;
        }
    }
    int ans = 1e9;
    for(int i = 0;i < s2.size();i++){
        ULL Hash = 0;
        for(int j = i;j < s2.size();j++){
            Hash = Hash * prime + s2[j];
            m2[j - i + 1][Hash]++;
        }
    }
    for(int i = 1;i <= min(s1.size(),s2.size());i++){
        for(auto it = m1[i].begin();it!=m1[i].end();it++){
            if(it->ss > 1)continue;
            ULL Hash = it->ff;
            if(m2[i][Hash] == 1){
                ans = min(ans,i);
            }
        }
    }
    if(ans == 1e9)cout << -1 << endl;
    else cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 5005;
unordered_map<int,int>m1,m2;
ULL prime = 3797;
ULL F[N],h1[N],h2[N];

ULL seg1(int x, int y) {
    return h1[y] - F[y - x + 1] * h1[x - 1];
}
ULL seg2(int x, int y) {
    return h2[y] - F[y - x + 1] * h2[x - 1];
}
char s1[N],s2[N];

int main()
{
    F[0] = 1; for(int i = 1; i < N; i++) F[i] = F[i - 1] * prime;

    scanf("%s",s1 + 1);
    scanf("%s",s2 + 1);
    int n = strlen(s1 + 1);
    int m = strlen(s2 + 1);

    h1[0] = 0; FOR(i, 1, n + 1) h1[i] = h1[i - 1] * prime + s1[i];
    h2[0] = 0; FOR(i, 1, m + 1) h2[i] = h2[i - 1] * prime + s2[i];

    int ans = -1;
    for(int mid = 1;mid <= min(n,m);mid++)
    {
        m1.clear();m2.clear();
        for(int i = 1;i + mid - 1 <= n;i++){
            m1[seg1(i,i+mid-1)]++;
        }
        for(int i = 1;i + mid - 1 <= m;i++){
            m2[seg2(i,i+mid-1)]++;
        }
        if(m1.size() > m2.size())swap(m1,m2);
        bool ok = 0;
        for(auto it = m1.begin();it!=m1.end();it++){
            if(it->ss != 1)continue;
            if(m2[it->ff] == 1){
                ok = 1;break;
            }
        }
        if(ok == 1){
            ans = mid;
            break;
        }
    }
    cout << ans << "\n";
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 5005;
unordered_map<ULL,int>m1[N],m2[N],m[N];
ULL prime = 3797;
int main()
{
    string s1,s2;
    cin >> s1 >> s2;
    for(int i = 0;i < s1.size();i++){
        ULL Hash = 0;
        for(int j = i;j < s1.size();j++){
            Hash = Hash * prime + s1[j];
            m[j - i + 1][Hash]++;
        }
    }
    for(int i = 1;i <= s1.size();i++){
        for(auto it = m[i].begin();it!=m[i].end();it++){
            if(it->ss == 1)m1[i][it->ff] = 1;
        }
        m[i].clear();
    }
    int ans = 1e9;
    for(int i = 0;i < s2.size();i++){
        ULL Hash = 0;
        for(int j = i;j < s2.size();j++){
            Hash = Hash * prime + s2[j];
            m2[j - i + 1][Hash]++;
        }
    }
    for(int i = 1;i <= s2.size();i++){
        for(auto it = m[i].begin();it!=m[i].end();it++){
            if(it->ss == 1)m2[i][it->ff] = 1;
        }
    }
    for(int i = 1;i <= min(s1.size(),s2.size());i++){
        for(auto it = m1[i].begin();it!=m1[i].end();it++){
            if(it->ss > 1)continue;
            ULL Hash = it->ff;
            if(m2[i][Hash] == 1){
                ans = min(ans,i);
            }
        }
    }
    if(ans == 1e9)cout << -1 << endl;
    else cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
const int N = 5005;
unordered_map<ULL,int>m1[N],m2[N];
ULL prime = 3797;
int main()
{
    string s1,s2;
    cin >> s1 >> s2;
    for(int i = 0;i < s1.size();i++){
        ULL Hash = 0;
        for(int j = i;j < s1.size();j++){
            Hash = Hash * prime + s1[j];
            m1[j - i + 1][Hash]++;
        }
    }
    int ans = 1e9;
    for(int i = 0;i < s2.size();i++){
        ULL Hash = 0;
        for(int j = i;j < s2.size();j++){
            Hash = Hash * prime + s2[j];
            m2[j - i + 1][Hash]++;
        }
    }
    for(int i = 1;i <= min(s1.size(),s2.size());i++){
        for(auto it = m1[i].begin();it!=m1[i].end();it++){
            if(it->ss > 1)continue;
            ULL Hash = it->ff;
            if(m2[i][Hash] == 1){
                ans = min(ans,i);
            }
        }
    }
    if(ans == 1e9)cout << -1 << endl;
    else cout << ans << endl;
}