#!/bin/bash

arrray=()
trainArray=()
stationArra=()

read str
ary=(`echo $str`)
n=${ary[0]}
m=${ary[1]}

p=0
nn=`expr $n + 1`
mm=`expr $m + 1`
size=$((nn * mm))
while [ $p -lt $size ];
do
 array[$p]=0
 trainArray[$p]=0
 stationArray[$p]=0
 p=`expr $p + 1`
done

j=1
val=1
while [ $j -le $n ];
do
 read str
 ary=(`echo $str`)
 l=${ary[0]}
 r=${ary[1]}
 i=$l
 while [ $i -le $r ];
  do
   mmf=$((mm * j))
   key=$((i + mmf))
   array[$key]=$val
   i=`expr $i + 1`
  done
  val=`expr $val + 1`
  j=`expr $j + 1`
done

s=1
while [ $s -le $m ];
do
 t=1
 while [ $t -le $n ];
 do
  mmf=$((mm * t))
  key=$((s + mmf))
  if [[ ${array[$key]} -ne 0 ]];
  then
   stationArray[$key]=1
  fi
 t=`expr $t + 1`
 done
 s=`expr $s + 1`
done

x=1
while [ $x -le $m ];
do
 y=1
 while [ $y -le $n ];
 do
  z=1
  mmfx=$((mm * x))
  keyx=$((y + mmfx))
  while [ $z -le $m ];
  do
  mmfz=$((mm * y))
  keyz=$((z + mmfz))
  if [ $((z % x)) -eq 0 -a ${stationArray[$keyz]} -eq 1 ];
  then
    trainArray[$keyx]=1
  fi
  z=`expr $z + 1`
  done
  y=`expr $y + 1`
 done
 x=`expr $x + 1`
done

u=1
while [ $u -le $m ];
do
 cnt=0
 v=1
 while [ $v -le $n ];
 do
  mmf=$((mm * u))
  key=$((v + mmf))
  trainval=${trainArray[$key]}
  cnt=$((cnt + trainval))
  v=`expr $v + 1`
 done
 echo $cnt
 u=`expr $u + 1`
done