/*
  Motto - try hard to become an expert
*/
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <vector>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <iomanip>
 
//define
#define rep(i,n)    for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define INF (int)INT_MAX
#define all(v)  v.begin(), v.end()
#define endl "\n"
 
//typedef
typedef long long int ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< ll > vll;
typedef std::vector< std::pair < pii, int > > vpp;
 
const int N = 3*1e5 +5 ;
const long long MOD = 1e9 + 7;
const long long inf = 1e18;
 
using namespace std;
 

ll modpow(ll x,ll n){
    ll res=1;
    while(n>0){
        if(n&1) res=res*x%MOD;
        x=x*x%MOD;
        n>>=1;
    }
    return res;
}
 
ll modFact(ll n, ll p) 
{ 
    if (n >= p) 
        return 0; 
  
    ll result = 1; 
    for (ll i = 1; i <= n; i++) 
        result = (result * i) % p; 
  
    return result;  
} 
 
bool isPrime(int x)
{
    if(x%2==0)
        return 0;
    for(int i=3;i<=sqrt(x);i+=2)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}
 
ll binomialCoeff(ll n, ll k) 
{ 
    ll C[n + 1][k + 1]; 
    ll i, j; 


    for (i = 0; i <= n; i++) 
    { 
        for (j = 0; j <= min(i, k); j++) 
        { 
            
            if (j == 0 || j == i) 
                C[i][j] = 1; 

            else
                C[i][j] = (C[i - 1][j - 1] + 
                                        C[i - 1][j])%MOD; 
        } 
    } 

    return C[n][k]; 
}
bool compare(const pair<int,int> &a , const pair<int,int>&b){
    //set<pair<int,int> , decltype(&compare)>s(&compare) ;
    if(a.fi!=b.fi)return a.se<b.se ;
    return a.fi<b.fi;
}

// struct compare{
//     // for priority_queue
//     bool operator()(const int &a , const int&b){
//         return a<b ;
//     }
// };

void solve()
{
    int n , m ;
    cin>>n>>m;
    vector<int>L(m) , R(m) ;
    rep(i , m){
        cin>>L[i]>>R[i] ;
    }
    int mal = INT_MIN ;
    int mir = INT_MAX ;
    rep(i , m){
        mal = max(L[i] , mal) ;
        mir = min(mir , R[i]) ;
    }
    int ans= 0 ;
    repu(i , 1 , n){
        if(mal<=i && mir>=i){
            ans++;
        }
    }
    cout<<ans<<endl;
}
 
int main() {
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
    
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    } 
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<vector<char>> G;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

long long bfs(int sx, int sy, vector<vector<bool>> &vis, G &g) {
  int h = g.size();
  int w = g[0].size();

  queue<pair<int, int>> que;
  que.push(make_pair(sx, sy));
  vis[sy][sx] = true;

  long long cnt_w = 0, cnt_b = 0;

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    int cur_x = cur.first;
    int cur_y = cur.second;

    if (g[cur_y][cur_x] == '#') {
      cnt_b++;
    } else {
      cnt_w++;
    }

    for (int i = 0; i < 4; i++) {
      int nx = cur_x + dx[i];
      int ny = cur_y + dy[i];

      if (nx < 0 || ny < 0 || nx >= w || ny >= h) {
        continue;
      }
      if (vis[ny][nx]) {
        continue;
      }
      if (g[cur_y][cur_x] == g[ny][nx]) {
        continue;
      }
      vis[ny][nx] = true;
      que.push(make_pair(nx, ny));
    }
  }
  return cnt_b * cnt_w;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> grid(h, vector<char>(w));

  for (int y = 0; y < h; y++) {
    string tmp_s;
    cin >> tmp_s;
    for (int x = 0; x < w; x++) {
      grid[y][x] = tmp_s[x];
    }
  }

  long long ans = 0;
  vector<vector<bool>> is_visited(h, vector<bool>(w, false));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (!is_visited[y][x]) {
        ans += bfs(x, y, is_visited, grid);
      }
    }
  }
  cout << ans << "\n";

  return 0;
}